PoshCode Archive  Artifact [1b7267b954]

Artifact 1b7267b95416b403a5e4fe58209c9312196e6cc189e9ef2b02142fe2d033388a:

  • File Get-WebConfigSqlConnectionString.ps1 — part of check-in [42824d9520] at 2018-06-10 13:25:33 on branch trunk — A function to get a sql connection string in a web.config file (user: unknown size: 1010)

# encoding: ascii
# api: powershell
# title: 
# description: A function to get a sql connection string in a web.config file
# version: 0.1
# type: function
# license: CC0
# function: Get-WebConfigSqlConnectionString
# x-poshcode-id: 3435
# x-archived: 2012-06-06T05:55:48
#
# See http://poshcode.org/536
#

function Get-WebConfigSqlConnectionString 
{
param( [switch]$help, [string]$configfile = $(read-host "Please enter a web.config file to read"), 
[string]$conname = $(read-host "Please enter connection name"));

  $usage = "Usage: Get-WebConfigSqlConnectionString -configfile c:\inetpub\wwwroot\web.config -conname 'ConName'";

  if ($help) {Write-Host $usage;break}

  $webConfigPath = (Resolve-Path $configfile).Path;
  $xml = [xml](get-content $webConfigPath);
  $root = $xml.get_DocumentElement();
  $connStrings = $root.connectionStrings;
  $addTag = $connStrings.add
  $conStringTag = $addTag | Where { $_.Name -eq $conname}
  return $conStringTag.connectionString
}