I saw a question on Facebook about how to get Test-WsMan to return a simple Boolean result. The Test-Connection cmdlet has a -Quiet parameter that makes this possible. But Test-Wsman does not. Certainly, you could script a comparable outcome. Here's one way:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
"chi-hvr2","chi-hvr1","chi-p50" | foreach { $r = Test-WSMan $_ -ErrorAction SilentlyContinue if ($r) { $True } else { $False } }
Or, if all you want is to simply filter a list of names you could use an expression like this:
"chi-hvr2","chi-hvr1","chi-p50" | Where { test-wsman $_ -ErrorAction SilentlyContinue}
The only names that come out the end will be those that responded to Test-WsMan. Or you can go another way and create your own version of Test-WsMan which is what I did.
I used my Copy-Command script to create a wrapper function I named Test-MyWsMan. I added a parameter called -Quiet so that the command behaves like Test-Connection. While I was at it, I also addressed some other shortcomings (for me at least) in Test-WsMan. When testing multiple computers, not having a computername property meant the results weren't that useful. And you could only get values in the ProductVersion property if you specified an Authentication parameter value. So I fixed these "issues".
You can find my new command on as a gist in GitHub.
I've also updated help and examples. Now, I have a more useful tool.
Now, I can filter like this:
Or even on the ProductVersion.
"chi-hvr2","chi-dc01","chi-dc02","chi-p50","chi-dc04" |
where { (Test-MyWsman -ComputerName $_).ProductVersion -notmatch "Stack: 2.0" } |
foreach { Get-CimInstance win32_operatingsystem -computername $_}
I can already see where it might be helpful to split up that ProductVersion property into separate properties. But I'll save that for another day.
In the meantime, I hope you'll grab a copy and let me know what you think. Enjoy your weekend, especially in the US where we have a long one.
UPDATE: I've published v2 on GitHub and briefly explained it at http://bit.ly/1ZjusCr. The code you see listed above may not completely match this article.
I’ve started falling in love with every piece of code you write! Great like always !! 🙂
Oh my! Thanks.
Excellent post. I was just explaining to a class on Tuesday why you might want to use a proxy function, and this is a perfect example.
Thanks. Although technically, this is not a proxy function. But I could have used my Copy-Command script to create one and make similar adjustments.