{"id":351,"date":"2006-12-07T06:00:00","date_gmt":"2006-12-07T14:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/07\/extending-types-okay-already\/"},"modified":"2006-12-07T06:00:00","modified_gmt":"2006-12-07T14:00:00","slug":"extending-types-okay-already","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/07\/extending-types-okay-already\/","title":{"rendered":"Extending Types &#8211; Okay, already"},"content":{"rendered":"<p><P>Having been asked plenty of times, here&#8217;s the type extension I added to create the CanPing property for the String type:<\/P><PRE>&nbsp;&lt;Type&gt;<BR>&nbsp;&nbsp;&lt;Name&gt;System.String&lt;\/Name&gt;<BR>&nbsp;&nbsp;&lt;Members&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ScriptProperty&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Name&gt;CanPing&lt;\/Name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;GetScriptBlock&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;$wmi = get-wmiobject -query &#8220;SELECT * FROM Win32_PingStatus WHERE Address = &#8216;$this'&#8221;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if ($wmi.StatusCode -eq 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$true<BR>&nbsp;&nbsp;&nbsp;&nbsp;} else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$false<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/GetScriptBlock&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;\/ScriptProperty&gt;<BR>&nbsp;&nbsp;&lt;\/Members&gt;<BR>&nbsp;&lt;\/Type&gt;<\/PRE><P>Note that this SHOULD be dropped into a standalone .ps1xml file as follows:<\/P><PRE>&lt;Types&gt;<BR>&nbsp;&lt;Type&gt;<BR>&nbsp;&nbsp;&lt;Name&gt;System.String&lt;\/Name&gt;<BR>&nbsp;&nbsp;&lt;Members&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;ScriptProperty&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Name&gt;CanPing&lt;\/Name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;GetScriptBlock&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;$wmi = get-wmiobject -query &#8220;SELECT * FROM Win32_PingStatus WHERE Address = &#8216;$this'&#8221;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if ($wmi.StatusCode -eq 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$true<BR>&nbsp;&nbsp;&nbsp;&nbsp;} else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$false<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/GetScriptBlock&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;\/ScriptProperty&gt;<BR>&nbsp;&nbsp;&lt;\/Members&gt;<BR>&nbsp;&lt;\/Type&gt;<BR>&lt;\/Types&gt;<\/PRE><P>And imported into PowerShell using the Update-TypeData cmdlet. Then, just do this:<\/P><PRE>[string]$name = &#8220;ServerA&#8221;<BR>$name.CanPing<\/PRE><P>To see if it&#8217;s pingable or not. Hmm&#8230; bet this could be adapted into a cmdlet-like function which takes a list of names and only outputs those which ARE pingable&#8230;<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having been asked plenty of times, here&#8217;s the type extension I added to create the CanPing property for the String type.<\/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-351","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\/351","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=351"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}