Continuing from where I left off last week, we were created an instance of the System.Net.NetworkInformation.Ping class and looked at the different ways (overloads) that we can call the Send method.  Now that we’ve seen how we can create Ping objects, let’s take advantage of options for creating some custom ping packets and examine the return object.

PS C:> $encoder = new-object System.Text.ASCIIEncoding

PS C:> $bytes = $encoder.Getbytes(‘Hello From Steve’)

PS C:> $response = $ping.Send(‘google.com’, 100, $bytes)

Let’s take a look at the response we get back.

PS C:> $response | Get-Member

   TypeName: System.Net.NetworkInformation.PingReply

Name                  MemberType     Definition —-                       ———-     ———- Equals                  Method         System.Boolean Equals(Object obj) GetHashCode        Method         System.Int32 GetHashCode() GetType               Method         System.Type GetType() get_Address          Method         System.Net.IPAddress get_Address() get_Buffer            Method         System.Byte[] get_Buffer() get_Options          Method         System.Net.NetworkInformation.PingOptions… get_RoundtripTime Method         System.Int64 get_RoundtripTime() get_Status            Method         System.Net.NetworkInformation.IPStatus ge… ToString               Method         System.String ToString() Address                Property       System.Net.IPAddress Address {get;} Buffer                  Property       System.Byte[] Buffer {get;} Options                Property       System.Net.NetworkInformation.PingOptions… RoundtripTime       Property       System.Int64 RoundtripTime {get;} Status                  Property       System.Net.NetworkInformation.IPStatus St… BufferSize             ScriptProperty System.Object BufferSize {get=$this.Buffe… OptionsString        ScriptProperty System.Object OptionsString {get=‘TTL={0}…

Get-Member shows us that this is a PingReply object in the System.Net.NetworkInformation namespace.

Looking at the PingReply object that was returned, we have several methods that look strangely similar to the properties, but with a “get_” prefixed to it.  When you see a method that matches a Property, but has “get_” or “set_”, that is a method exposed by the .NET Framework that provides the functionality for the properties.  When you access the Buffer property, you are actually calling the get_Buffer method.  Properties are, for the most part, easier to work with and you can effectively ignore those methods.  Properties also contain a clue in their description, if you look at the end of the Buffer property’s description, you see “{get;}” which indicates that there is a way to retrieve that property.  Properties can also show “{set;}” which indicates that you can update that property.

In PowerShell V2 CTP3 those “getters” and “setters” (as those methods are commonly called) are hidden by default.  If you are running CTP3 and want to see the “getters” and “setters”, you can call Get-Member with the Force parameter.

PS C:> $response | Get-Member –Force

Back to the properties of our return object.  The properties names are pretty self-explanatory, but to confirm we got back the “special” packet we crafted, let’s take a look at the Buffer property and convert that back to text.

PS C:> $encoder.GetString($response.buffer) Hello From Steve

Now for the meat of the response, the RoundtripTime and Status.  I commonly use the Roundtrip time to watch for latency on my network, and since it is exposed so nicely, it is easy to log.

PS C:> $response.RoundtripTime 74

And of course, we are interested in the Status.

PS C:> $response.Status Success

Now, we can see from the Get-Member return above, that Status is not just a string that says, “Success”, it is a IPStatus enumeration (more on enumerations coming up, but long story short, there are a number of predefined values that an IPStatus object can hold)(also found in the System.Net.NetworkInformation namespace). 

I’m normally checking the IPStatus for a Success value like

PS C:> $success = [System.Net.NetworkInformation.IPStatus]::Success

PS C:> $response.status -eq $success True

I could compare the string results, but then I’m stepping back to parsing text results.  By using the IPStatus enumeration values, I can get be reasonably sure that my value are correct and I don’t have any text matching issues.

In the next post in this series, we’ll take a look at a special type of method, the constructor.