简体   繁体   中英

Adding color to results of PowerShell scripts Exception

I am fairly new to the Powershell world and having a bit of difficulty figuring this issue out. My code is below and pretty simple, compared to others I have done this with, but this one just will not work and I can't figure out what I have done wrong. I have done almost the exact same thing using much longer and more complex "Lists" to start from than the simple $VMToolsList I have below. When I run the code below I get the following error for both wsIndex1 and 2. Any idea of what I am missing?

Exception calling "IndexOf" with "2" argument(s): "Value cannot be null. Parameter name: array" At 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 }
    }
}

Start by debugging why $VMToolsxml.Descendants("${Namespace}th").Value is resulting in a null. BTW XLinq and PowerShell don't work that great together. Descendants is an extension method which PowerShell does not automatically support. You would use the extension method this way:

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

I would consider using PowerShell's support of System.Xml.XmlDocument and use Select-Xml with an XPath query to find your nodes.

In Line 9 you are using the XContainer.Descendants Method (XName) . This method returns a IEnumerable Interface which does not appear to support the .Value method, which is why I suspect it is returning null.

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

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

Just an amateur trying to help, hope this is on the right track.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM