The powershell queue collection

01-03 22:00

Today I want to quickly talk about the systems.collections.Queue collection.

why should we care about the system.collections.Queue collection

Working with powershell, generally a simple array declared and used like this will generally be enough.

$array = @()
$array += "Stéphane"
$array += "van Gulick"

We then need to access our array via the index

PS C:\WINDOWS\system32> $array[1]
van Gulick

Although this is trivial, and very often enough for our daily business (or in other words, we will always find a way to make it work). there are sometime some other (better?) ways to do things.

The System.Collection.Queue ist like a regular array, but is has some convenient methods that allow us to always return the  first element added to our queue.

The System.Collection.queue follows the principle of  first in, first out.

Methods and properties you don’t want to miss:

Lets have a look at the members of our collection object:

$q = New-Object System.Collections.Queue
$q | gm # throws an error

Pretty surprisingly, when you try to look at the members that are available you will find your self in front of the following error:

gm : You must specify an object for the Get-Member cmdlet.
At line:1 char:6
+ $q | gm # throws an error
+      ~~
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

I’ll explain a bit later what really happened here, but for now, know that although we have had this error message, and when we look into our $q  variable it seems empty, the instance has been created.  We will can see it members if we use the  -inputObject  parameter, as in the example below.

PS C:\WINDOWS\system32> get-member -InputObject $q
 
 
   TypeName: System.Collections.Queue
 
Name           MemberType Definition                                                                                                
----           ---------- ----------                                                                                                
Clear          Method     void Clear()                                                                                              
Clone          Method     System.Object Clone(), System.Object ICloneable.Clone()                                                   
Contains       Method     bool Contains(System.Object obj)                                                                          
CopyTo         Method     void CopyTo(array array, int index), void ICollection.CopyTo(array array, int index)                      
Dequeue        Method     System.Object Dequeue()                                                                                   
Enqueue        Method     void Enqueue(System.Object obj)                                                                           
Equals         Method     bool Equals(System.Object obj)                                                                            
GetEnumerator  Method     System.Collections.IEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator()
GetHashCode    Method     int GetHashCode()                                                                                         
GetType        Method     type GetType()                                                                                            
Peek           Method     System.Object Peek()                                                                                      
ToArray        Method     System.Object[] ToArray()                                                                                 
ToString       Method     string ToString()                                                                                         
TrimToSize     Method     void TrimToSize()                                                                                         
Count          Property   int Count {get;}                                                                                          
IsSynchronized Property   bool IsSynchronized {get;}                                                                                
SyncRoot       Property   System.Object SyncRoot {get;}

We will focus on the three most interesting ones of the Queue collection:

  • Enqueue
  • Dequeue
  • peek

The .Enqueue() method of the system.collections.Queue

The enqueue method is the method that will allow us to add new information to our queue.

$q.Enqueue("hi")
$q.Enqueue("How are")
$q.Enqueue("you ?")
$q
 
hi
How are
you ?

Notice the order in which the strings are appearing.

Pretty straight forward I would say.

The DeQueue() method

The dequeue method is the method that will return the next element of our queue.

Remember, the systems.collections.Queue is a type of collection that respects the  first in, First out principle

PS C:\WINDOWS\system32> $q.Dequeue()
hi

As you can see, “ hi ” is the  first element to be returned when we called the  DeQueue() method.

When we look again in our variable, we can see that the value hi is not more present anymore, but  “how are” and “ you ” are still there.

PS C:\WINDOWS\system32> $q
How are
you ?

The Peek() method:

The peek method will work exactly as the Dequeue() method, except,  that the item that was returned will  not be removed from the  Queue . As it’s name suggest, it allows you to  peek into the queue, and to  see what would   be the next returned element if you would call the  DeQueue() method.

PS C:\WINDOWS\system32> $q.Peek()
How are

As you can see in the example above, the peek method ( in red ) returns the item, but  doesn’t removes it from the stack collection.

Using the pop method, returns the “gulick” item just as the  peek method informed us it would do, and in this case, removed it from the  stack item.

Links

Official MSDN Documentation –>   here

Github issue explaining why $q | Get-Member doesn’t work –> here

That’s is all for today

原文链接:http://powershelldistrict.com/powershell-queue-collection/?utm_source=tuicool&utm_medium=referral
标签: PowerShell Windows
© 2014 TuiCode, Inc.