2

私はPowershellの世界にかなり慣れていないため、この問題を理解するのに少し苦労しています. 私のコードは以下にあり、これを行った他のものと比較して非常に単純ですが、これは機能せず、何が間違っているのかわかりません。以下に示す単純な $VMToolsList よりもはるかに長く複雑な「リスト」を使用して、ほぼまったく同じことを行いました。以下のコードを実行すると、wsIndex1 と 2 の両方で次のエラーが発生します。

"2" 個の引数を指定して "IndexOf" を呼び出す例外: "値を null にすることはできません。パラメーター名: 配列" C:\Users\xxxxxxxxxxx\AppData\Local\Temp\f2dfef29-9e86-4193-9c37-98b35015e97f.ps1 で:9 char:2 + $wsIndex1 = [配列]::IndexOf( $VMToolsxml.Descendants("${Namespace}th").Value, ... + ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) []、MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException

Add-Type -AssemblyName System.Xml.Linq

New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine -ValueFromExtensionProperty 'Config.tools.ToolsVersion' -Force 
New-VIProperty -Name ToolsVersionStatus -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.ToolsVersionStatus' -Force
$VMToolsList = $(Get-VM | Select Name, Version, ToolsVersion, ToolsVersionStatus)

$VMToolsxml = [System.Xml.Linq.XDocument]::Parse( "$($VMToolsList | ConvertTo-Html)" )

$wsIndex1 = [Array]::IndexOf( $VMToolsxml.Descendants("${Namespace}th").Value, "Version")
$wsIndex2 = [Array]::IndexOf( $VMToolsxml.Descendants("${Namespace}th").Value, "ToolsVersionStatus")

foreach($row in $VMToolsxml.Descendants("${Namespace}tr")){
    switch(@($row.Descendants("${Namespace}td"))[$wsIndex1]) {
       {"v7" -eq $_.Value } { $_.SetAttributeValue( "style", "background: green;"); continue } 
       {"v7" -ne $_.Value } { $_.SetAttributeValue( "style", "background: red; font color: black"); continue } 
    }
    switch(@($row.Descendants("${Namespace}td"))[$wsIndex2]) {
       {"guestToolsCurrent" -eq $_.Value } { $_.SetAttributeValue( "style", "background: green;"); continue } 
       {"guestToolsNeedUpgrade" -eq $_.Value } { $_.SetAttributeValue( "style", "background: yellow; font color: black"); continue }
       {"guestToolsNotInstalled" -eq $_.Value } { $_.SetAttributeValue( "style", "background: red; font color: black"); continue }
       {"guestToolsUnmanaged" -eq $_.Value } { $_.SetAttributeValue( "style", "background: purple;"); continue }
    }
}
4

2 に答える 2

1

$VMToolsxml.Descendants("${Namespace}th").Valuenull になる理由をデバッグすることから始めます。ところで、XLinq と PowerShell は連携してうまく機能しません。子孫は、PowerShell が自動的にサポートしない拡張メソッドです。拡張メソッドを次のように使用します。

[System.Xml.Linq.Extensions]::Descendants($VMToolsxml, "${Namespace}th")

PowerShell の System.Xml.XmlDocument のサポートを使用することを検討し、XPath クエリで Select-Xml を使用してノードを見つけます。

于 2013-08-29T00:15:26.107 に答える