我正在尝试从connectionstrings.config文件中获取信息并将其填充到CSV中。 到目前为止,我有以下脚本:
$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
我卡住的部分,不确定要做什么是如何使用for [$ i]variables来创build不同的CSV列,使用位于[$ i]variables中的数据。 所以,我正在检查我的文件有多less个连接string,然后使用这个数组#来生成variables。 然后,我去在Powershell的不同列中填充这些variables。 所以,数组[0]将有密码[0]。 我想使用foreach循环,但没有运气。
请帮忙或build议。 您的帮助深表谢意。
无需手动创buildCSV,PowerShell具有ConvertTo-CSV和Export-Csv cmdlet为您完成!
如果我理解你的代码,那么这个例子应该像下面这样简单:
$Filename = "C:\new.config" [xml]$config = Get-Content $fileName $config.connectionStrings | Export-Csv -Path $home\desktop\test.csv