Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: A Better Test-WsMan

Posted on May 27, 2016June 8, 2016

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:

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

image

Now, I can filter like this:

image

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 $_}

image

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

4 thoughts on “Friday Fun: A Better Test-WsMan”

  1. Prateek Singh says:
    May 27, 2016 at 10:31 am

    I’ve started falling in love with every piece of code you write! Great like always !! 🙂

    1. Jeffery Hicks says:
      May 27, 2016 at 10:49 am

      Oh my! Thanks.

  2. Mike Shepard says:
    May 27, 2016 at 11:00 am

    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.

    1. Jeffery Hicks says:
      May 27, 2016 at 11:02 am

      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.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d