我在一个文本文件中有这个 XML 文档:
<?xml version="1.0"?>
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="DisplayName" Type="System.String">SQL Server (MSSQLSERVER)</Property>
<Property Name="ServiceState" Type="Microsoft.SqlServer.Management.Smo.Wmi.ServiceState">Running</Property>
</Object>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="DisplayName" Type="System.String">SQL Server Agent (MSSQLSERVER)</Property>
<Property Name="ServiceState" Type="Microsoft.SqlServer.Management.Smo.Wmi.ServiceState">Stopped</Property>
</Object>
</Objects>
我想遍历每个对象并找到DisplayName
and ServiceState
。我该怎么做?我已经尝试了各种组合并且正在努力解决它。
我这样做是为了将 XML 放入一个变量中:
[xml]$priorServiceStates = Get-Content $serviceStatePath;
$serviceStatePath
上面显示的 xml 文件名在哪里。然后我想我可以做类似的事情:
foreach ($obj in $priorServiceStates.Objects.Object)
{
if($obj.ServiceState -eq "Running")
{
$obj.DisplayName;
}
}
在这个例子中,我想要一个输出的字符串SQL Server (MSSQLSERVER)