# 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
}
