{"id":48,"date":"2007-01-14T05:00:00","date_gmt":"2007-01-14T13:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/14\/make-a-cmdlet-part-5-more-fixes\/"},"modified":"2007-01-14T05:00:00","modified_gmt":"2007-01-14T13:00:00","slug":"make-a-cmdlet-part-5-more-fixes","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/14\/make-a-cmdlet-part-5-more-fixes\/","title":{"rendered":"Make-A-Cmdlet: Part 5, More Fixes"},"content":{"rendered":"<p><font style=\"color: #000000\" color=\"#000000\">So my cmdlet still isn&#8217;t working. This time, I&#8217;ve made another &#8220;debugging&#8221; change to my code, adding this line:<\/font><\/p>\n<pre><font style=\"color: #000000\" color=\"#000000\">For Each ThisName In _Name<br \/>&nbsp;WriteDebug(&quot;Attempting to ping &quot; &amp; ThisName)<br \/><strong>&nbsp;WriteObject(&quot;SELECT StatusCode FROM Win32_PingStatus WHERE Address = '&quot; &amp; ThisName &amp; &quot;'&quot;)<\/strong>&nbsp;Dim Query As SelectQuery<br \/>&nbsp;Query = New SelectQuery(&quot;SELECT StatusCode FROM Win32_PingStatus WHERE Address = '&quot; &amp; ThisName &amp; &quot;'&quot;)<\/font><\/pre>\n<p><font style=\"color: #000000\" color=\"#000000\">As you can see, I&#8217;ve just added a line to output my WMI query. This results in the following when I run the cmdlet in PowerShell:<\/font><\/p>\n<pre><font style=\"color: #000000\" color=\"#000000\">PS C:\\&gt; get-content c:\\computers.txt | ping-computer<br \/>SELECT StatusCode FROM Win32_PingStatus WHERE Address = 'sapienteched'<br \/>sapienteched<br \/>SELECT StatusCode FROM Win32_PingStatus WHERE Address = 'server2'<br \/>server2<br \/>SELECT StatusCode FROM Win32_PingStatus WHERE Address = 'localhost'<br \/>localhost<br \/>SELECT StatusCode FROM Win32_PingStatus WHERE Address = ''<\/font><\/pre>\n<p><font style=\"color: #000000\" color=\"#000000\">Now I can copy and paste the exact query my code is using into Wbemtest.exe, to test it. I want to try the Server2 query, since I know Server2 should NOT be pingable. When I try it, the query works&#8230; but I learn something interesting. The StatusCode property, which is usually 0 for a successful ping, is Null for Server2&#8217;s ping. Looking at my code:<\/font><\/p>\n<pre><font style=\"color: #000000\" color=\"#000000\">If result.GetPropertyValue(&quot;StatusCode&quot;) = 0 Then<\/font><\/pre>\n<p><font style=\"color: #000000\" color=\"#000000\">I see my mistake. I&#8217;m comparing Null to 0, and anything compared to Null is Null&#8230; in other words, this comparison doesn&#8217;t equal True or False, ir equals Null&#8230; which is messing up the logic in my code. I need to use VB.NET&#8217;s IsNothing() function to see if the returned value is null or not. Doing so, I recompile, rerun, etc, and get this:<\/font><\/p>\n<pre><font style=\"color: #000000\" color=\"#000000\">PS C:\\&gt; add-pssnapin primaltoys<br \/>PS C:\\&gt; get-content c:\\computers.txt | ping-computer<br \/>sapienteched<br \/>localhost<\/font><\/pre>\n<p><font style=\"color: #000000\" color=\"#000000\">Notice that I&#8217;ve removed my debugging code that wrote out the query; this WORKS! This is what I want! Yay! Here&#8217;s my final code for the entire cmdlet (BTW, I know this formatting is a little crazy &#8211; hang in because at the end of the series I&#8217;ll post the project files; I don&#8217;t want to do so until I&#8217;m DONE, though):<\/font><\/p>\n<p><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Imports<\/font><font style=\"color: #000000\" color=\"#000000\"> System.Management.Automation<\/font><\/font><\/p>\n<p><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Imports<\/font><font style=\"color: #000000\" color=\"#000000\"> System.Management<\/font><\/font><\/p>\n<p><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">&lt;Cmdlet(VerbsDiagnostic.Ping, <\/font><font style=\"color: #000000\" color=\"#000000\">&#8220;Computer&#8221;, SupportsShouldProcess:=True)&gt; _<\/font><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Public <\/font><font style=\"color: #000000\" color=\"#000000\">Class<\/font><\/font><font size=\"2\"><font style=\"color: #000000\" face=\"Courier New,Courier,mono\" color=\"#000000\" size=\"3\"> PingComputerCmd<\/font><\/font><\/font><font face=\"Courier New,Courier,mono\"><font size=\"2\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Inherits<\/font><font style=\"color: #000000\" color=\"#000000\"> Cmdlet <br \/>&#8216;&lt;Parameter(Position:=0, Mandatory:=False)&gt; _ <\/font><\/font><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">&#8216;Public Property Name() As String <font style=\"color: #000000\" color=\"#000000\">&#8217; Get <font style=\"color: #000000\" color=\"#000000\">&#8217; Return &#8220;&#8221; <font style=\"color: #000000\" color=\"#000000\">&#8217; End Get <font style=\"color: #000000\" color=\"#000000\">&#8217; Set(ByVal value As String) <font style=\"color: #000000\" color=\"#000000\">&#8217; End Set <font style=\"color: #000000\" color=\"#000000\">&#8216;End Property <\/font><font style=\"color: #000000\" color=\"#000000\">Private _Name AsString<\/font><font style=\"color: #000000\" color=\"#000000\">() <\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><font size=\"3\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\"><font style=\"color: #000000\" color=\"#000000\"><font style=\"color: #000000\" color=\"#000000\"><font style=\"color: #000000\" color=\"#000000\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">&lt;Parameter(Position:=0, Mandatory:=<\/font><font style=\"color: #000000\" color=\"#000000\">False, ValueFromPipeline:=True<\/font><font style=\"color: #000000\" color=\"#000000\">)&gt; _<\/font><\/font><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Public Property Name() AsString<\/font><font style=\"color: #000000\" color=\"#000000\">() <br \/>Get <font style=\"color: #000000\" color=\"#000000\">Return<\/font><font style=\"color: #000000\" color=\"#000000\"> _Name <br \/>End Get <font style=\"color: #000000\" color=\"#000000\">Set(ByVal value AsString<\/font><font style=\"color: #000000\" color=\"#000000\">()) <\/font><\/font><font style=\"color: #000000\" face=\"Courier New,Courier,mono\" color=\"#000000\" size=\"3\">_Name = value<font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">End Set <font style=\"color: #000000\" color=\"#000000\">End <\/font><font style=\"color: #000000\" color=\"#000000\">Property <\/font><font size=\"2\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Protected OverridesSub<\/font><font style=\"color: #000000\" color=\"#000000\"> ProcessRecord()<br \/>Dim ThisName As String<font style=\"color: #000000\" color=\"#000000\">For Each ThisName In<\/font><font style=\"color: #000000\" color=\"#000000\"> _Name <\/font><\/font><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">WriteDebug(<\/font><font style=\"color: #000000\" color=\"#000000\">&#8220;Attempting to ping &#8220;<\/font><font style=\"color: #000000\" color=\"#000000\"> &amp; ThisName)<\/font><\/font><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Dim Query As<\/font><font style=\"color: #000000\" color=\"#000000\"> SelectQuery <\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><font size=\"3\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\"><font style=\"color: #000000\" face=\"Courier New,Courier,mono\" color=\"#000000\" size=\"3\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\"><font size=\"2\"><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font size=\"3\"><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Query = <\/font><font style=\"color: #000000\" color=\"#000000\">New SelectQuery(&#8220;SELECT StatusCode FROM Win32_PingStatus WHERE Address = &#8216;&#8221; &amp; ThisName &amp; &#8220;&#8217;&#8221;<\/font><font style=\"color: #000000\" color=\"#000000\">)<\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Dim Searcher As<\/font><font style=\"color: #000000\" color=\"#000000\"> ManagementObjectSearcher <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Searcher = <\/font><font style=\"color: #000000\" color=\"#000000\">New<\/font><font style=\"color: #000000\" color=\"#000000\"> ManagementObjectSearcher(Query)<\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">Dim result As<\/font><font style=\"color: #000000\" color=\"#000000\"> ManagementObject<br \/>For Each result In<\/font><font style=\"color: #000000\" color=\"#000000\"> Searcher.Get<br \/>If Not IsNothing(result.GetPropertyValue(&#8220;StatusCode&#8221;)) Then <font style=\"color: #000000\" color=\"#000000\">If result.GetPropertyValue(&#8220;StatusCode&#8221;) = 0 Then <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">WriteDebug(<\/font><font style=\"color: #000000\" color=\"#000000\">&#8220;Successful ping of &#8220;<\/font><font style=\"color: #000000\" color=\"#000000\"> &amp; ThisName)<\/font><\/font><\/font><\/p>\n<p><font style=\"color: #000000\" face=\"Courier New,Courier,mono\" color=\"#000000\" size=\"3\">WriteObject(ThisName)<\/font><\/p>\n<p><font style=\"color: #000000\" face=\"Courier New,Courier,mono\" color=\"#000000\" size=\"3\">Else <\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">WriteDebug(<\/font><font style=\"color: #000000\" color=\"#000000\">&#8220;Could not ping &#8220;<\/font><font style=\"color: #000000\" color=\"#000000\"> &amp; ThisName)<\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">End If<font style=\"color: #000000\" color=\"#000000\">Else <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">WriteDebug(<\/font><font style=\"color: #000000\" color=\"#000000\">&#8220;Could not ping &#8220;<\/font><font style=\"color: #000000\" color=\"#000000\"> &amp; ThisName)<\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">End If<br \/>&nbsp;<\/font><font style=\"color: #000000\" color=\"#000000\">Next<font style=\"color: #000000\" color=\"#000000\">Next<font style=\"color: #000000\" color=\"#000000\">End <\/font><font style=\"color: #000000\" color=\"#000000\">Sub <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font face=\"Courier New,Courier,mono\"><font style=\"color: #000000\" color=\"#000000\">End <\/font><font style=\"color: #000000\" color=\"#000000\">Class<\/font><\/font><\/font><\/p>\n<p><font style=\"color: #000000\" face=\"Times New Roman,Times,serif\" color=\"#000000\" size=\"3\">But stay tuned&#8230; there&#8217;s still&nbsp;a bit more to learn. And apologies for the formatting&#8230; neither Visual Studio nor the blog software really seem to like one another when it comes to copy\/paste&#8230; again, I&#8217;ll be posting the source code at the end of this series, so just hang in there.<\/font><\/font><\/font> <\/p>\n<p>&nbsp;<\/p>\n<p><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p><P>So my cmdlet still isn&#8217;t working. This time, I&#8217;ve made another &#8220;debugging&#8221; change to my code, adding this line:<\/P\n\n\n<\/p>\n","protected":false},"author":2,"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":[2,25],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/48","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}