我是PowerShell的新手。通过一些参考,我为下面的场景编写了一些脚本。
如果有更好的方法可以建议我。
我在文件“ Test1.xml”中具有以下XML数据:
<TextValuess>
<TextValues Name="Value1" Override="true" Type="String">
<DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
<INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
<INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
<INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
</TextValues>
<TextValues Name="ENVIRONMENT" Override="true" Type="String">
<DEV>DEV</DEV>
<INT10>INT10</INT10>
<INT>INT1</INT>
<INT2>INT15</INT2>
</TextValues>
</TextValuess>
我能够使用下面的PowerShell脚本读取所需的数据。但是我无法将值分配给PowerShell变量。
我的Powershell脚本是:
[xml]$XmlDocument = Get-Content D:\Roshan\Test1.xml
$child = 'INT10'
$XmlDocument.SelectNodes("//TextValues[./${child}]") | ForEach-Object {
New-Object -Type PSObject -Property @{
'Name' = $_.Name
'Value' = $_.$child
}
}
我需要一些输入/建议,如何将这些XML值传递给存储过程。
[xml]$XmlDocument = Get-Content D:\Roshan\Test1.xml
$child = 'INT10'
#用于连接SQL Server的命令(我已经完成了此操作)
$XmlDocument.SelectNodes("//TextValues[./${child}]") | ForEach-Object {
New-Object -Type PSObject -Property @{
$Name = $_.Name #Need to Assign Value to a Variable $Name
$Value = $_.$child
# I Want to call a stored procedure from XML here.
}
}
答案 0 :(得分:0)
对于为什么需要使用已收集的属性创建变量的说法也感到困惑,但这可能对您有用:
$child = 'INT10'
[xml]$XmlDocument = @"
<TextValuess>
<TextValues Name="Value1" Override="true" Type="String">
<DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
<INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
<INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
<INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
</TextValues>
<TextValues Name="ENVIRONMENT" Override="true" Type="String">
<DEV>DEV</DEV>
<INT10>INT10</INT10>
<INT>INT1</INT>
<INT2>INT15</INT2>
</TextValues>
</TextValuess>
"@
$result = $XmlDocument.SelectNodes("//TextValues/$child") | ForEach-Object {
New-Object -Type PSObject -Property @{
# I don't know what you want in 'Name'.. Do you mean the 'Name' attribute from the parent node (TextValues) ?
'ParentName' = $_.ParentNode.Name # --> "Value1"
'Name' = $_.LocalName # --> "INT10"
'Value' = $_.InnerText # --> "Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;"
}
}
$result
现在是带有PSObjects的数组:
ParentName Name Value ---------- ---- ----- Value1 INT10 Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI; ENVIRONMENT INT10 INT10
要使用各种属性并将它们放在变量中(尽管您可以按原样使用它们。)
foreach ($item in $result) {
$Name = $item.Name # or $item.ParentName if that is what you are after
$Value = $item.Value
# do something with the variables
}