Fix Test-WSMan

I've encountered a number of PowerShell (v4) sessions recently which did not return expected results from the Test-WSMan command, when run against the local OS. To workaround this hiccup, I've come up wit the following 1-liner (strictly technically speaking) to provide the same info, and as a PowerShell custom object.

The info, in this case, comes from the winrm id command, which outputs pretty much the exact same format as Test-WSMan. We then -split and parse the output to place the OS, SP, and Stack info into their own properties.

For example, on my test machine, Test-WSMan returns the following:

PS .\Test-WSMan
wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

The part that bothers me is 'ProductVersion'. In numerous cases, I'm seeing these results with 0's for OS and SP.

Compared with the raw output from winrm id (on the same machine), I get:

PS .\winrm id
IdentifyResponse
ProtocolVersion = http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor = Microsoft Corporation
ProductVersion = OS: 6.1.7601 SP: 1.0 Stack: 3.0

Notice how now we get valid numbers back for OS: and SP:, as well as Stack:.

Ultimately, I want to be able to compare/check the values of the OS, SP, and Stack attributes, so I'm parsing the ProductVersion line (with -split ' '). Big thanks to Alistair Pialek for showing/reminding me how to pull out the elements from the -split array.

So here's what I've pulled together:

$Private:WinRMVersion = & winrm id; $Private:WinRMVerTokens = $WinRMVersion -split '='; $Private:ProductVerTokens = $WinRMVerTokens[6] -split ' ';$Private:properties = [ordered]@{'ProductVersion'=$WinRMVerTokens[6];'OS'=$ProductVerTokens[2];'SP'=$ProductVerTokens[4];'Stack'=$ProductVerTokens[6]};$Private:WinRMID = New-Object -TypeName PSObject -Property $properties;$WinRMID

You can also see this code as a GitHUb Gist

Here's an example of the output:

ProductVersion                      OS          SP      Stack
--------------                      --          --      -----
OS: 6.1.7601 SP: 1.0 Stack: 3.0     6.1.7601    1.0     3.0

It can be a little nicer to look at (at least on this post) if you pipe it through Format-List ...

ProductVersion :  OS: 6.1.7601 SP: 1.0 Stack: 3.0
OS             : 6.1.7601
SP             : 1.0
Stack          : 3.0