3 April 2016

A coworker pinged me the other day needing help with a short script he was running on a C# web page. He was pulling worker group information from Citrix and wanted to sort them based off of the number of servers in each worker group. There isn’t a property on the object returned from Get-XAWorkergroup that includes a server count. He said he googled it and couldn’t find anything online about sorting by the count of a property on an object. I thought he was crazy (both in wanting to sort by server count and that he couldn’t find anything) so I gave it a quick search myself. Surprisingly, there isn’t anything.

I thought for a moment how I would go about doing this and first looked into measuring and object and seeing if I could pipe that to be sorted. Doesn’t work. After wishing “if only there was a property for server count” I remembered the Add-Member cmdlet and the ability to add properties to an object. I love this feature of Powershell and how easy it is to do. It’s more complicated in the programming environment because you’d need to create a new class or extend the existing one. It’s so easy and flexible to do it on the fly when you need to.

I first started by trying to pipe the array of my objects to Add-Member but that failed. Add-Member doesn’t appear to be “smart enough” to recognize you are piping an array of objects to it and so it acts on the array object itself. It wasn’t until after 10 minutes of frustration and realizing this that I figured out why my calculating server count kept coming back as 0. The array itself doesn’t have a property “ServerNames” so it couldn’t give me a count! Powershell can be confusing if you’re not paying attention in that Get-Member (GM) tells you the members of the objects in the array, it doesn’t give information about what the variable is. Always use the GetType() method to determine what exactly your variable is. So, after modifying it slightly I did a ForEach on the array, and inside the ForEach I used Add-Member to get an accurate server count and then add it as a property to each object. After that, it’s just a matter of piping it to sort. Here is the final code, one line:

add-pssnapin citrix.xenapp.commands; get-xaworkergroup -comp socxa65pddc01 | foreach { add-member -InputObject $_ -NotePropertyName ServerCount -NotePropertyValue $_.ServerNames.count -Passthru } | sort -property ServerCount -descending

The one thing to keep in mind is that Add-Member does not output anything by default so you have to use -PassThru so that you have something to pipe to sort!


There are no comments.



You must be logged in to post a comment.

Links

RSS 2.0 Feed

Support

Brave Rewards
This site supports Brave Rewards. Please consider tipping or adding it to your monthly contributions if you find anything helpful!

For other ways: Support

Support this blog! If you have found it helpfu you can send me crypto. Any amount is appreciated!
ETH: 0xBEaF72807Cb5f4a8CCE23A5E7949041f62e8F8f0 | BTC: 3HTK5VJWr3vnftxbrcWAszLkRTrx9s5KzZ | SHIB: 0xdb209f96dD1BdcC12f03FdDfFAD0602276fb29BE
Brave Users you can send me BAT using the browser.