2013-08-28 39 views
2

我對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 } 
    } 
} 
+0

我還應該補充一點,我有這種輸出到電子郵件和結果報告確實通過適當填寫所有預期的數據,只是沒有彩色格式。是的,我確實已添加了vmware管理單元,並且與vcenter的連接處於活動狀態。 – user2727009

回答

1

開始調試爲什麼$VMToolsxml.Descendants("${Namespace}th").Value導致null。 BTW XLinq和PowerShell不能很好地協同工作。後裔是PowerShell不會自動支持的擴展方法。你可以使用擴展方法是這樣的:

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

我會考慮使用PowerShell的支持System.Xml.XmlDocument和使用選擇的XML與XPath查詢找到您的節點。

+0

感謝指針基思。現在我睡了一晚後有了一些新鮮的眼睛,我再次看到它爲什麼會返回null,正如你所建議的那樣,那就是它了。我忽略了定義命名空間。一旦我添加了下面的代碼,剛剛定義$ VMToolsxml之後,它就像冠軍一樣工作。 '如果($命名空間= $ xml.Root.Attribute( 「XMLNS」)。值){$ 命名空間= 「{{{0}}}」 -f $命名空間 \t}' – user2727009

0

在第9行中,您正在使用XContainer.Descendants方法(XName)。此方法返回一個IEnumerable接口它似乎不支持.Value方法,這就是爲什麼我懷疑它返回null。

http://msdn.microsoft.com/en-us/library/bb360635.aspx

http://msdn.microsoft.com/en-us/library/9eekhta0.aspx

只是一個業餘愛好者想幫忙,希望這是在正確的軌道上。

+0

由於JLA,這看起來像一些很好的閱讀。看起來我會在腳本中做更多這樣的事情,所以我不認爲我可以避免進入並理解我現在正在查看的內容。 – user2727009