PowerShell is Fun. Yes it really is ! If it doesn’t give you a straightforward way to get things done, there is always a workaround. To substantiate my point, let me give you a scenario I recently came across. At Microsoft, I am a part of a Team that helps Enterprise Customers embrace Microsoft Cloud Solutions. For example, we help our customers migrate from their OnPremise Messaging Infrastructure (Lotus Notes / Exchange) to Office 365. Recently while working with a Customer, we noticed heavy network latency issues from the Customer’s end thereby resulting into slow mailbox moves. Their network backbone decided to take a day off thereby delaying their journey to the cloud. The Customer wanted to know what was the average data transferred per minute.

That’s simple! I exclaimed. The Get-MoveRequestStatistics CmdLet has the property “BytesTransferredPerMinute” that does exactly what it is named for. Let’s take a peek at the Get-MoveRequestStatistics CmdLet and see this in action. I have intentionally skipped the Alias and DisplayName Property of the Users for obvious confidentiality reasons. That’s irrelevant to the topic in discussion Smile

 

image

Cool !. Now that we have the Property, we can simply Pipeline it to the Measure-Object CmdLet and calculate the Average Bytes Transferred Per Minute. This is what it would look like.

image

 

Arrrrrrrrrrrgh! Red Text…The dreaded red text… Something is wrong here. But as all PowerShell Gurus say, read the errors and not “Clear Screen” them Smile. It is clear from the error displayed that the BytesTransferredPerMinute is not of the Integer Data Type and hence PowerShell cannot compute its average.

image

To get more clarity, let’s use the Get-Member CmdLet (One of the three weapons to master PowerShell. If you are wondering what the other two are, they are Get-Command and Get-Help)

image

 

As you can notice, while doing an implicit remoting to the cloud, the objects returned are deserialized. Serialization and Deserialization are topics that I would cover in a different blog post. So the question remains, can we actually calculate the Average Bytes Transferred Per Minute. Yeah, probably by copying the results in an Excel Spreadsheet and juggling around it till we get the numbers and using the Average Function. Naah, that’s not PowerShellish, isn't it? Smile

 

To do this with PowerShell, we can convert the values into String Objects and then use the Methods the String Objects support to get our desired result. One of the values returned was 4.886 MB (5,123,667 bytes). What we are interested is the Part before the “MB”, in other words the Numbers, which in this case would be 4.866

This is an ideal case where we can use the SubString() and the IndexOf() methods of the String Object to get our desired output. Using the IndexOf method we will find the Index value of MB and then we will use the SubString() method to only extract the part starting from the first character at position 0 to the Index of MB, which is nothing but the numbers we are interested. This is illustrated in the screenshot below:

 

image

 

Now, lets bind all these together to get the Average Bytes Transferred Per Minute. This is demonstrated in the Screenshot Below

image

 

Code:

gc .\Users.txt | Get-MoveRequestStatistics | Select @{l="BytesTransferredPerMin(MB)";e={$_.BytesTransferredPerMinute.ToString().SubString(0,($_.BytesTransferredPerMinute.ToString().IndexOf("MB")))}} `

| Measure-Object -Property 'BytesTransferredPerMin(MB)' -Average -Sum -Maximum -Minimum

 

Even though it looks confusing, it actually isn't. What I did with the $var example above, I applied the same logic here. The only difference is that we used the ToString() Method to convert the value of BytesTransferredPerMinute Property to a String Object.

And that’s exactly what the Customer wanted. As always, we can play around the number formatting as detailed here and get more defined results. The same logic can also be applied for calculating the Average Data Transferred (BytesTransferred).

This might just be one way of getting the results we wanted. There might be others as well. Smile

 

Hope you found this post useful.

Till then, Happy PowerShelling Smile