PowerShell Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "Q" and there wasn't really much to go with.  We've got the "-Quiet" switch but that's fairly limited in it's usage.  I opted for it's support for the FIFO data structure: queues.

queue A queue is a particular kind of collection in which the entities in the collection are kept in order.  Operation on a queue are to add items to the end position and remove them from the front position. 

As a First-In-First-Out (FIFO) data structure, the first element added to the queue will be the first one to be removed.

There are many types of Queues that you can work with, including native .NET Queues, Windows Azure Queues, and IBM WebSphere MQs.


PowerShell doesn't have a native queue data type but that doesn't stop you from being able to build and manage one as the .NET framework has done that for us and since PowerShell has access to all of the .NET Framework classes, working with a queue is as easy as calling the New-Object Cmdlet on a System.Collections.Queue object.

PS C:\> $q = New-Object System.Collections.Queue
PS C:\> $q.Enqueue("one")
PS C:\> $q.Enqueue("two")
PS C:\> $q.Enqueue("three")
PS C:\> $q
PS C:\> $q.Dequeue()
PS C:\> $q
PS C:\> $q.Enqueue("four")
PS C:\> $q

Windows Azure Queues

Let's not stop with local queues, PowerShell allows you to access them in the cloud as well.  Windows Azure is the foundation of Microsoft's Cloud Platform and provides the building blocks for application developers to write scalable and highly available services.   One of those building blocks is the Windows Azure Queue which provides a reliable message delivery mechanism.  Microsoft has provided a "CloudDrive" sample that implements a PowerShell Provider to to enable command line access to Queue Storage resources.

PS C:\> queue:
PS queue:\> Get-ChildItem messagequeue
Name Msg Cat
---- -------
messagequeue 1
PS queue:\> $q = (dir messagequeue)
PS queue:\> $q | Get-Member
TypeName: Microsoft.Samples.ServiceHosting.StorageClient.QueueRest
Name MemberType Definition
---- ---------- ----------
Length AliasProperty Length = Size
Path AliasProperty Path = QueueUri
add_MessageReceived Method System.Void add_MessageReceived(MessageReceivedEv...
ApproximateCount Method System.Int32 ApproximateCount()
Clear Method System.Boolean Clear()

IBM WebSphere MQs

There are also other PowerShell libraries for Queuing systems out there.  IBM has released their Cmdlet's for WebSphere MQ.  Here's a great list of blog posts on a beginners guide to PowerShell for WebSphere MQ if you are interested in getting started.  With IBM's latest release, you can now manage queue managers across multiple servers from a single PowerShell workstation.  Here's a link to the PowerShell for IBM WebSphere MQ documentation.