I have few connectionstrings which are xml files. I need to generate a CSV report using all these connectionstrings with the following output:
Below is my PS script which works fine for 1 file and gives me the desired output. I have been playing around to make it work for all the files
function extractValues ($string, $values){
return ($values | %{ $string -match "$_=([^;]+)" | Out-Null; $matches} | %{$_[1]})
}
[string]$Value1 = '/connectionStrings/add[@name="Value1"]/@connectionString'
[string]$Value2 = '/connectionStrings/add[@name="Value2"]/@connectionString'
[string]$Value3 = '/connectionStrings/add[@name="Value3"]/@connectionString'
$files = Get-Childitem –Path C:\inetpub\wwwroot\*\ConfigSections\connectionStrings.config.New.config
foreach ($file in $files) {
[xml]$xml = Get-Content $file
$connectionString = ($xml | Select-Xml -XPath $Value1).ToString()
($serverName, $dbName, $user) = extractValues $connectionString @("Data Source", "Initial Catalog", "User ID")
$connectionString = ($xml | Select-Xml -XPath $Value2).ToString()
($EserverName, $EdbName, $Euser) = extractValues $connectionString @("Data Source", "Initial Catalog", "User ID")
$connectionString = ($xml | Select-Xml -XPath $Value3).ToString()
($SCSserverName, $SCSdbName, $SCSuser) = extractValues $connectionString @("Data Source", "Initial Catalog", "User ID")
$table=@"
Initial Catalog = $Value1,
Value1, connectionstring=Data Source=$serverName, Initial Catalog=$dbName, User ID=$user
Value2, connectionstring=Data Source=$EserverName, Initial Catalog=$EdbName, User ID=$Euser
Value3, connectionstring=Data Source=$SCSserverName, Initial Catalog=$SCSdbName, User ID=$SCSuser
"@
#export to a csv file
$table | Set-Content $home\desktop\test.csv
}
This only outputs csv for 1 connectionstring. Not sure, what am I missing.
Thank you