xml - Adding color to results of PowerShell scripts Exception -
i new powershell world , having bit of difficulty figuring issue out. code below , pretty simple, compared others have done with, 1 not work , can't figure out have done wrong. have done exact same thing using longer , more complex "lists" start simple $vmtoolslist have below. when run code below following error both wsindex1 , 2. idea of missing?
exception calling "indexof" "2" argument(s): "value cannot null. parameter name: array" @ 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 debugging why $vmtoolsxml.descendants("${namespace}th").value
resulting in null. btw xlinq , powershell don't work great together. descendants extension method powershell not automatically support. use extension method way:
[system.xml.linq.extensions]::descendants($vmtoolsxml, "${namespace}th")
i consider using powershell's support of system.xml.xmldocument , use select-xml xpath query find nodes.
Comments
Post a Comment