0

I am trying to grab information from the connectionstrings.config file and populate it to a CSV. I have the following script so far:

$Filename = "C:\new.config"
[xml]$config = Get-Content $fileName

$CSCount = $config.connectionStrings.add.connectionString.count

[array]$CS = $config.connectionStrings.add.connectionString
$CScount = $CS.count
$CSCount
for($i=0; $i -lt $CScount; $i++)
{
    New-Variable -Name "CS$i" -Value $CS[$i]
    New-Variable -Name "Password$i" -value ($CS[$i].Split(';') | 
    ConvertFrom-StringData).'Password'
}


$table=@"
foreach ($C in $CSCount) { connectionstring[$i] }
$Password[$i]
"@
#export to a csv file
$table | Set-Content $home\desktop\test.csv

The part where I am stuck and not sure what to do is how to use the for [$i] variable to create different CSV columns using the data located in the [$i] variable. So, I am checking how many connectionstrings does my file has and then use this array # to generate variables. Then, I went to populate these variable in different columns in Powershell. So, array[0] would have Password[0]. I thought of using foreach loop, but no luck.

Please help or suggest. Your help is deeply appreciated.

1

No need to create the CSV manually, PowerShell has the ConvertTo-CSV and Export-Csv cmdlets to do it for you!

If I understand your code the example should be as simple as:

$Filename = "C:\new.config"
[xml]$config = Get-Content $fileName
$config.connectionStrings | Export-Csv -Path $home\desktop\test.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.