{"id":106,"date":"2007-04-09T11:00:00","date_gmt":"2007-04-09T19:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/04\/09\/ismanageable\/"},"modified":"2007-04-09T11:00:00","modified_gmt":"2007-04-09T19:00:00","slug":"ismanageable","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/04\/09\/ismanageable\/","title":{"rendered":"IsManageable"},"content":{"rendered":"<p>One issue that always seems to come up when using WMI to manage remote machines is being able to communicate with WMI on the remote machine. This is especially an issue when the remote server sits behind a firewall or across a WAN. I got tired of running complex WMI queries only to have them fail because the computer is unavailable.&nbsp; I need a way to validate the computer before carrying out the main part of any script or automation task.<\/p>\n<p>Pinging the computer is not a valid test as sometimes network devices are configured to block that traffic. Or, just because a computer does respond to a ping, doesn&#8217;t mean I can use WMI.&nbsp; Ping uses ICMP and WMI uses RPC.&nbsp; Totally different protocols.&nbsp; <\/p>\n<p>What I came up with is a PowerShell function I call IsManageable.&nbsp; I know it probably doesn&#8217;t fit the Powershell naming convention so feel free to rename it.&nbsp; But it makes sense to me. The function will return True if it can establish a WMI connection or False if it can&#8217;t.<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Con\" color=\"#0000ff\" size=\"2\"><\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">Function IsManageable {<br \/>param([string]$Computer,`<br \/>[System.Management.Automation.PSCredential]$credential) <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if ($credential -IS [System.Management.Automation.PSCredential]) <br \/>{<br \/>#use alternate credentials if supplied<br \/>$sb={(Get-WmiObject win32_operatingsystem -computer $Computer `<br \/>-credential $credential -erroraction SilentlyContinue).caption} <br \/>}<br \/>else {<br \/>#use existing credentials<br \/>$sb={(Get-WmiObject win32_operatingsystem -computer $Computer `<br \/>-erroraction SilentlyContinue).caption}<br \/>} <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if (&amp;$sb -IS [STRING])<br \/>{<br \/>Return $TRUE<br \/>}<br \/>else<br \/>{<br \/>Return $FALSE<br \/>}<br \/>} #end function<\/font> <\/p>\n<p>The function takes a computername as a run time parameter, as well as a PSCredential object. The credential is an optional parameter. The preferred method is to specify an alternate credential before using this function like this:<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">$cred=Get-Credential(&#8220;domain\\administrator&#8221;)<\/font><\/p>\n<p>Then run<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; IsManageable DC01 $cred<\/font><\/p>\n<p>You could also use this expression which will prompt you for alternate credentials:<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; IsManageable DC01 (get-credential)<\/font><\/p>\n<p>The function checks the second parameter and if it is a PSCredential, then it creates a Get-WMIObject script block that uses -credential:<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if ($credential -IS [System.Management.Automation.PSCredential]) <br \/>{<br \/>#use alternate credentials if supplied<br \/>$sb={(Get-WmiObject win32_operatingsystem -computer $Computer `<br \/>-credential $credential -erroraction SilentlyContinue).caption} <br \/>}<br \/><\/font><\/p>\n<p>Otherwise, the script block is created without it:<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">else {<br \/>#use existing credentials<br \/>$sb={(Get-WmiObject win32_operatingsystem -computer $Computer `<br \/>-erroraction SilentlyContinue).caption}<br \/>} <\/font><\/p>\n<p>The last part of the function checks the value of the script block and if it is a [String} type, <\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if (&amp;$sb -IS [STRING])<\/font><\/p>\n<p>then I know a value was returned for the operating system caption and thus the computer is manageable via WMI. By using a script block, it makes my function a little easier to read. <\/p>\n<p>You&#8217;ll also notice that I&#8217;ve set the -erroraction parameter for the Get-Wmiobject cmdlet to SilentlyContinue. This is because I want to cmdlet to finish, even if an error is raised. I&#8217;m assuming that if an error is raised, that the computer is not manageable and the function will return False. You will still get an error if you try to access a machine without the proper credentials, which I suppose tells you the computer is manageable, provided you use alternate credentials.<\/p>\n<p>Even if you don&#8217;t need this function, I hope it serves as a useful demonstration of the -IS operator and how to use scriptblocks.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:45d4611f-0f51-459e-95ba-97e253aa92fd\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\"><span class=\"sizeLess20\">Technorati tags: <\/span><a href=\"http:\/\/technorati.com\/tags\/PowerShell\" rel=\"tag\"><span class=\"sizeLess20\">PowerShell<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Functions\" rel=\"tag\"><span class=\"sizeLess20\">Functions<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/WMI\" rel=\"tag\"><span class=\"sizeLess20\">WMI<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Get-WMIObject\" rel=\"tag\"><span class=\"sizeLess20\">Get-WMIObject<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\"><span class=\"sizeLess20\">Scripting<\/span><\/a><\/div>\n<p><\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:2bc9584e-6e57-42a8-b5e4-2195fa848e2f\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\"><span class=\"sizeLess20\">del.icio.us tags: <\/span><a href=\"http:\/\/del.icio.us\/popular\/PowerShell\" rel=\"tag\"><span class=\"sizeLess20\">PowerShell<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/del.icio.us\/popular\/Functions\" rel=\"tag\"><span class=\"sizeLess20\">Functions<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/del.icio.us\/popular\/WMI\" rel=\"tag\"><span class=\"sizeLess20\">WMI<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/del.icio.us\/popular\/Get-WMIObject\" rel=\"tag\"><span class=\"sizeLess20\">Get-WMIObject<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/del.icio.us\/popular\/Scripting\" rel=\"tag\"><span class=\"sizeLess20\">Scripting<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>One issue that always seems to come up when using WMI to manage remote machines is being able to communicate with WMI on the remote machine. This is especially an issue when the remote server sits behind a firewall or across a WAN. I got tired of running complex WMI queries only to have them fail because the computer is unavailable.  I need a way to validate the computer before carrying out the main part of any script or automation task.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}