我对 Powershell 世界还很陌生,想弄清楚这个问题有点困难。我的代码在下面并且非常简单,与我做过的其他代码相比,但是这个代码不起作用,我不知道我做错了什么。与下面的简单 $VMToolsList 相比,我使用更长和更复杂的“列表”开始几乎完全相同的事情。当我运行下面的代码时,wsIndex1 和 2 都出现以下错误。知道我缺少什么吗?
使用“2”参数调用“IndexOf”的异常:“值不能为空。参数名称:数组”在 C:\Users\xxxxxxxxxxx\AppData\Local\Temp\f2dfef29-9e86-4193-9c37-98b35015e97f.ps1 :9 char:2 + $wsIndex1 = [Array]::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 }
}
}