私は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 }
}
}