我對Powershell世界相當陌生,並且在解決此問題時遇到了一些困難。我的代碼是低於和非常簡單,相比,我做了這些,但這只是不會工作,我不知道我做錯了什麼。我已經做了幾乎完全相同的事情,使用比我下面簡單的$ VMToolsList更長,更復雜的「列表」開始。當我運行下面的代碼時,我得到了wsIndex1和2的以下錯誤:任何想法我缺少什麼?爲PowerShell腳本的結果添加顏色異常
異常調用 「的IndexOf」 與 「2」 的參數(一個或多個): 「值不能爲空 參數名:數組」 在C:\用戶\ XXXXXXXXXXX \應用程序數據\本地\ 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 }
}
}
我還應該補充一點,我有這種輸出到電子郵件和結果報告確實通過適當填寫所有預期的數據,只是沒有彩色格式。是的,我確實已添加了vmware管理單元,並且與vcenter的連接處於活動狀態。 – user2727009