{"id":38,"date":"2007-01-07T02:00:00","date_gmt":"2007-01-07T10:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/07\/make-a-cmdlet-part-4-crud\/"},"modified":"2007-01-07T02:00:00","modified_gmt":"2007-01-07T10:00:00","slug":"make-a-cmdlet-part-4-crud","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/07\/make-a-cmdlet-part-4-crud\/","title":{"rendered":"Make-A-Cmdlet: Part 4&#8230; Crud."},"content":{"rendered":"<p><P>Well, my Ping-Computer cmdlet didn&#8217;t work. Dang. But this is a useful opportunity to cover some debugging techniques! I&#8217;ve got a lot of places where things COULD be going wrong. When I run Ping-Computer, no matter what name I provide, I get no output &#8211; no result at all. That&#8217;s bad. So, I want to change something to see what the problem is. I want to change the cmdlet so that, no matter WHAT happens, SOME output gets created. Here&#8217;s my code revision:<\/P><PRE>For Each result In Searcher.Get<BR> If result.GetPropertyValue(&#8220;StatusCode&#8221;) Then<BR>  WriteDebug(&#8220;Successful ping of &#8221; &amp; ThisName)<BR>  WriteObject(ThisName)<BR> Else<BR>  WriteDebug(&#8220;Could not ping &#8221; &amp; ThisName)<BR>  <STRONG><EM>WriteObject(&#8220;NO&#8221;)<\/EM><\/STRONG><BR> End If<BR>Next<\/PRE><P>I only added ONE line of code, which I&#8217;ve highlighted above. I shut down PowerShell and re-build my cmdlet, generating a fresh DLL. Then I reopen PowerShell, and try again:<\/P><PRE>PS C:\\&gt; ping-computer localhost<BR>NO<BR>PS C:\\&gt;<\/PRE><P>Cool! I got output! So my cmdlet sort of works &#8211; but it&#8217;s not pinging correctly. That&#8217;s really good news, because I&#8217;ve narrowed the problem down to just one or two lines of code. So, the first thing I notice is that the WMI query in my cmdlet code is using a variable. I need to see what&#8217;s inside of that variable &#8211; another quick cmdlet modification will tell me:<\/P><PRE>For Each ThisName In _Name<BR>&nbsp;WriteDebug(&#8220;Attempting to ping &#8221; &amp; ThisName)<BR>&nbsp;<STRONG>WriteObject(&#8220;Attempting to ping &#8221; &amp; ThisName)<\/STRONG>&nbsp;<BR> Dim Query As SelectQuery<\/PRE><P><FONT size=2><FONT size=3>You may be asking why the WriteDebug() isn&#8217;t sufficient. Well, the host I&#8217;m using to run this doesn&#8217;t actually display what&#8217;s been output to WriteDebug&#8230; so I&#8217;ve just duplicated the line using WriteOutput, just so I can see what&#8217;s going on. Now, I recompile, rerun PowerShell, and try again:<\/FONT><\/FONT><\/P><PRE><FONT color=#000000>PS C:\\&gt; ping-computer localhost<BR>Attempting to ping localhost<BR>NO<BR>PS C:\\&gt;<\/FONT><\/PRE><P><FONT color=#000000>Don&#8217;t forget: You&#8217;ll need to re-add the PSSnapIn when you restart PowerShell. And my test proves that my variable is being populated correctly for my WMI query. Cool. But&#8230; it still isn&#8217;t working right. Oh, wait&#8230; DUH. I see the problem, now:<\/FONT><\/P><PRE><FONT color=#000000>If result.GetPropertyValue(&#8220;StatusCode&#8221;) Then<\/FONT><\/PRE><FONT color=#0000ff size=2><FONT face=\"Times New Roman,Times,serif\" color=#000000 size=3>This should be: <\/FONT><P><FONT face=\"Courier New,Courier,mono\" color=#000000 size=3>If result.GetPropertyValue(&#8220;StatusCode&#8221;) = 0 Then<\/FONT><\/P><P><FONT face=\"Times New Roman,Times,serif\" color=#000000 size=3>Instead. So, I&#8217;ll fix that and take out my little test code, rebuild, relaunch, re-add, and try again:<\/FONT><\/P><PRE><FONT face=\"Courier New,Courier,mono\" color=#000000 size=3>PS C:\\&gt; add-pssnapin primaltoys<BR>PS C:\\&gt; ping-computer localhost<BR>localhost<BR>PS C:\\&gt;<\/FONT><\/PRE><P><FONT face=\"Times New Roman,Times,serif\" color=#000000 size=3>AWESOME!! It worked, outputting the name of the successfully-pinged computer. Why wouldn&#8217;t I just have it output &#8220;True&#8221; or something? Here&#8217;s why: Supposed I have a file named C:\\Computers.txt, listing one computer name on each line of the file. Like this:<\/FONT><\/P><PRE><FONT face=\"Courier New,Courier,mono\" color=#000000 size=3>sapienteched<BR>server2<BR>localhost<\/FONT><\/PRE><P><FONT face=\"Times New Roman,Times,serif\" color=#000000 size=3>Then I could do this:<\/FONT><\/P><PRE><FONT face=\"Courier New,Courier,mono\" color=#000000 size=3>PS C:\\&gt; get-content c:\\computers.txt | ping-computer<BR>sapienteched<BR>server2<BR>localhost<\/FONT><\/PRE><PRE><FONT face=\"Courier New,Courier,mono\" color=#000000 size=3>PS C:\\&gt;<\/FONT><\/PRE><P><FONT face=\"Times New Roman,Times,serif\" color=#000000 size=3>Oh, wait&#8230; it was only supposed to output names it could ping, and Server2 shouldn&#8217;t have been on the list. I don&#8217;t have a Server2. Dang again &#8211; back to debugging&#8230; in my next installment!<\/FONT><\/P><P>&nbsp;<\/P><P>&nbsp;<\/P><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, my Ping-Computer cmdlet didn&#8217;t work. Dang. But this is a useful opportunity to cover some debugging techniques! I&#8217;ve got a lot of places where things COULD be going wrong. When I run Ping-Computer, no matter what name I provide, I get no output &#8211; no result at all. That&#8217;s bad. So, I want to change something to see what the problem is. I want to change the cmdlet so that, no matter WHAT happens, SOME output gets created. Here&#8217;s my code revision:<\/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-38","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\/38","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=38"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}