Ho questo documento XML in un file di testo:Come eseguire l'iterazione tramite XML in PowerShell?
<?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>
voglio scorrere ogni oggetto e trovare il DisplayName
e ServiceState
. Come potrei farlo? Ho provato tutti i tipi di combinazioni e sto lottando per risolverlo.
sto facendo questo per ottenere il codice XML in una variabile:
[xml]$priorServiceStates = Get-Content $serviceStatePath;
dove $serviceStatePath
è il nome del file xml mostrato sopra. Allora ho pensato che avrei potuto fare qualcosa di simile:
foreach ($obj in $priorServiceStates.Objects.Object)
{
if($obj.ServiceState -eq "Running")
{
$obj.DisplayName;
}
}
E in questo esempio vorrei una stringa emesso con SQL Server (MSSQLSERVER)
ho aggiunto qualche spiegazione. – mswietlicki
Puoi anche navigare nel documento usando la sintassi della proprietà familiare: '($ xml.Objects.Object |? {$ _. ServiceState -eq" Running "}). DisplayName' – zneak