Extending Types – Okay, already

Having been asked plenty of times, here’s the type extension I added to create the CanPing property for the String type:

 <Type>
  <Name>System.String</Name>
  <Members>
   <ScriptProperty>
    <Name>CanPing</Name>
    <GetScriptBlock>
    $wmi = get-wmiobject -query “SELECT * FROM Win32_PingStatus WHERE Address = ‘$this'”
    if ($wmi.StatusCode -eq 0) {
     $true
    } else {
     $false
    }
    </GetScriptBlock>
   </ScriptProperty>
  </Members>
 </Type>

Note that this SHOULD be dropped into a standalone .ps1xml file as follows:

<Types>
 <Type>
  <Name>System.String</Name>
  <Members>
   <ScriptProperty>
    <Name>CanPing</Name>
    <GetScriptBlock>
    $wmi = get-wmiobject -query “SELECT * FROM Win32_PingStatus WHERE Address = ‘$this'”
    if ($wmi.StatusCode -eq 0) {
     $true
    } else {
     $false
    }
    </GetScriptBlock>
   </ScriptProperty>
  </Members>
 </Type>
</Types>

And imported into PowerShell using the Update-TypeData cmdlet. Then, just do this:

[string]$name = “ServerA”
$name.CanPing

To see if it’s pingable or not. Hmm… bet this could be adapted into a cmdlet-like function which takes a list of names and only outputs those which ARE pingable…