<# .SYNOPSIS Hashtable Contains method sample using PowerShell .DESCRIPTION This script create and populates a hash table then demonstrates use of contains, containskey and containsvalue methods. .NOTES File Name : Contains-HashTable.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://pshscripts.blogspot.co.uk/2008/07/contains-hashtableps1.html .EXAMPLE PS c:\foo> .\Contains-HashTable.ps1 The Hashtable contains the following values: Name Value ---- ----- 4 four 0 zero 2 two 1 one 3 three The key "0" is in the hashtable: True The key "5" is in the hashtable: False The key "2" is in the hashtable: True The key "6" is in the hashtable: False The value "three" is in the hash table: True The value "nine" is in the hash table: False #> ## # Start of script ## $myht = @{} $myht.Add( 0, 'zero' ); $myht.Add( 1, 'one' ); $myht.Add( 2, 'two' ); # or a more powershell way $myht += @{'3'= 'three'} $myht += @{'4'='four' } # Display the values of the Hashtable 'The Hashtable contains the following values:' $myht '' # use contains method $myKey = 0; "The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.Contains( $myKey ) $myKey = 5; "The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.Contains( $myKey ) '' # Now use ContainsKey $myKey = 2; "The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.ContainsKey( $myKey ) $myKey = 6; "The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.ContainsKey( $myKey ) '' # Searches for a specific value using ContainsValue $myValue = 'three'; "The value `"{0}`" is in the hash table: {1}" -f $myValue, $myHT.ContainsValue( $myValue ) $myValue = 'nine'; "The value `"{0}`" is in the hash table: {1}" -f $myValue, $myHT.ContainsValue( $myValue ) # End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 14 July 2008
Contains-Hashtable.ps1
Clone-Hashtable.ps1
<# .SYNOPSIS Demonstrates the use of the .clone method with hash tables .DESCRIPTION This script creates and populates a hash table, then demosmtrates the use of the .clone() method. .NOTES File Name : Clone-HashTable.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS C:\foo> .\Clone-HashTable.ps1 There are 4 in the $openwith hash table as follows Name Value ---- ----- bmp paint.exe rtf wordpad.exe txt notepad.exe dib paint.exe There are 4 in the $newtable hash table as follows bmp paint.exe rtf wordpad.exe txt notepad.exe dib paint.exe #> ## # start of script ## # create new hash table $openWith = @{} # Add some elements to the hash table. $openWith.Add("txt", "notepad.exe") $openWith.Add("dib", "paint.exe") # Add in a powershell way! $openwith += @{"bmp"="paint.exe"} # or $wordpad = "wordpad.exe" $openwith += @{"rtf"=$wordpad} # display hash table "There are {0} in the `$openwith hash table as follows:" -f $openwith.count $openwith "" # now clone the table $newtable = $openwith.clone() # display new hash table "There are {0} in the `$newtable hash table as follows:" -f $newtable.count $newtable # End of script
Labels:
add,
powershell,
scripts,
system.collections.hashtable
Sunday, 13 July 2008
Clear-Hashtable.ps1
<# .SYNOPSIS Demonstrates Hashtable's Clear Method .DESCRIPTION This script creates and populates a hash table then demonstrates use of the .clear method. .NOTES File Name : Clear-HashTable.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS C:\foo> .\ClearHashTable.ps1 Initially, Count : 5 Values: Name Value ---- ----- five jumped one The two quick three brown four fox After Clear, Count : 0 Values: #> ## # Start of Script ## # Create and populate a hash table $MyHashTable= @{} $MyHashTable.Add( "one", "The" ) $MyHashTable.Add( "two", "quick" ) $MyHashTable.Add( "three", "brown" ) # Add using the PowerShell way: :-) $MyHashTable += @{"four"="fox" } $myHashTable += @{"five"="jumped"} # Display the count and values of the Hashtable "Initially," " Count : {0}" -f $MyHashTable.Count " Values:" $MyHashTable # Clear the Hashtable. $MyHashTable.Clear() "" # Display the count and values of the Hashtable "After Clear," " Count : {0}" -f $MyHashTable.Count " Values:" $MyHashtable # End of script
Labels:
clear,
powershell,
scripts,
system.collections.hashtable
Saturday, 12 July 2008
Get-GuidByByteString
<# .SYNOPSIS Creates and displays a GUID as a byte String .DESCRIPTION This script first creates a guid, then displays it as a byte string .NOTES File Name : Get-GuidByteString.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://pshscripts.blogspot.co.uk/2008/07/get-guidbybytestring.html .EXAMPLE PSH [C:\foo]: .\Get-GuidByteString.ps1' The GUID: 85d9e3b9-b414-481a-a773-57ca98ae893c converted to byte array is: 185 227 217 133 20 180 26 72 167 115 87 202 152 174 137 60 #> # Create a new GUID $g = [system.guid]::newguid() # Get guid into byte array $b=$g.tobytearray() # Finally display the byte array 'The GUID: {0} converted to byte array is:' -f $g $b
Labels:
newguid,
powershell,
scripts,
system.guid,
tobytearray
Add-Hashtable.ps1
<# .SYNOPSIS Demonstrates adding items to a hashtable .DESCRIPTION This script creates a Hashtable, then adds some items to it, and finally prints out the resulting hash table. .NOTES File Name : Add-Hashtable.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\Add-Hashtable.ps1 The Hashtable contains the following: Name Value ---- ----- four fox two quick three brown one The #> ## # Start of script ## # Create a hash table $MyHashTable = @{} # Add to it using .NET $MyHashTable.Add( 'one', 'The' ) $MyHashTable.Add( 'two', 'quick') # Add the PowerShell Way: $MyHashTable += @{'three' = 'brown'} $MyHashTable += @{'four' = 'fox' } # Display the Hashtable. ' The Hashtable contains the following:' $MyHashTable
Labels:
add,
powershell,
scripts,
system.collections.hashtable
Show-QueueProperties.ps1
<# .SYNOPSIS Demonstrates using a queue object .DESCRIPTION This script creates a queue, then adds some items to it, print out the contents then synchs it. .NOTES File Name : show-queueproperties.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 Tags : showqueue,enqueue,synchronize,system.collections.queue .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\show-queueproperties.ps1 Creating and populating $myq object with... 3 entries in the queue as follows: entry 0: Hello entry 1: World entry 2: Jerry Garcia Rocks!! Report on $myq $myq is synchronised? :False Synchronising $myq to $sq $sq is synchronised? :True #> ## # Start of script ## # Create new queue object $myq = new-object System.Collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") # or a more powershell way $myq += "Jerry Garcia Rocks!!" # Show use of the properties of the Queue # First use count property "Creating and populating `$myq object with..." $i=0 "`{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" # Now check synchronisation "Report on `$myq" "`$myq is synchronised? :{0}" -f $myq.IsSynchronized "" # Synch the queue "Synchronising `$myq to `$sq" $sq = [system.collections.queue]::synchronized($myq) # report again "`$sq is synchronised? :{0}" -f $sq.IsSynchronized # End Script
Labels:
powershell,
scripts,
system.collections.hashtable
Wednesday, 9 July 2008
Use-Queue.ps1
<# .SYNOPSIS Demonstrates the use of the .NET Queue object .DESCRIPTION This script implemented a sample of this objct using PowerShell .NOTES File Name : Use-Queue.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\Use-Queue.ps1 $myq object at start: 3 entries in the queue as follows: entry 0: Hello entry 1: World entry 2: PowerShell Rocks $myq after addding two items: 5 entries in the queue as follows: entry 0: Hello entry 1: World entry 2: PowerShell Rocks entry 3: Added queue object 1 entry 4: Added queue object 2 $myq after removing three items: 2 entries in the queue as follows: entry 0: Added queue object 1 entry 1: Added queue object 2 Peeked item $peek = Added queue object 1 $myq after peek: 2 entries in the queue as follows: entry 0: Added queue object 1 entry 1: Added queue object 2 #> ## # Start of script ## # Create new queue object $myq = new-object system.collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") $myq.Enqueue("PowerShell Rocks") # Show queue "`$myq object at start:" $i=0 "{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" #Enqueue two objects $myq.enqueue("Added queue object 1") $myq.enqueue("Added queue object 2") # Show queue "`$myq after addding two items:" $i=0 "{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" #now dequeue 3 objects $dq1=$myq.dequeue() $dq2=$myq.dequeue() $dq3=$myq.dequeue() # Show queue "`$myq after removing three items:" $i=0 "{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" #Peek at first item $peek=$myq.peek() "Peeked item `$peek = $peek" # Show queue after peek "`$myq after peek:" $i=0 "{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } # End of scriptcd
Monday, 7 July 2008
Clear-Queue.ps1
<# .SYNOPSIS Demonstrates a .NET Queue object .DESCRIPTION This script creates a queue, adds some items to it, and displays the queue. Finally, the clear method is called and results displayed. .NOTES File Name : clear-queue.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 Tags : system.collections.queue, enque, powershell, scripts .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\clear-queue.ps1 $myq object 3 entries in the queue as follow: entry 0: Hello entry 1: World entry 2: ! $myq object after the clear: 0 entries in the queue as follow queue is empty #> ## # Start of script ## # Create new queue object $myq = new-object System.Collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") $myq.Enqueue("!") # Display the properties and values of the Queue "`$myq object" $i=0 "`{0} entries in the queue as follow:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" # Now clear the queue $myq.clear() #show resutls "`$myq object after the clear:" $i=0 "`{0} entries in the queue as follow" -f $myq.Count if ($myque.count -gt 0) {$myq | % {"entry {0}: {1}" -f $i++,$_ }} else {"queue is empty"} # End of Script<# .SYNOPSIS Demonstrates a .NET Queue object .DESCRIPTION This script creates a queue, adds some items to it, and displays the queue. Finally, the clear method is called and results displayed. .NOTES File Name : clear-queue.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 Tags : system.collections.queue, enque, powershell, scripts .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\clear-queue.ps1 $myq object 3 entries in the queue as follow: entry 0: Hello entry 1: World entry 2: ! $myq object after the clear: 0 entries in the queue as follow queue is empty #> ## # Start of script ## # Create new queue object $myq = new-object System.Collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") $myq.Enqueue("!") # Display the properties and values of the Queue "`$myq object" $i=0 "`{0} entries in the queue as follow:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" # Now clear the queue $myq.clear() #show resutls "`$myq object after the clear:" $i=0 "`{0} entries in the queue as follow" -f $myq.Count if ($myque.count -gt 0) {$myq | % {"entry {0}: {1}" -f $i++,$_ }} else {"queue is empty"} # End of Script
Labels:
powershell,
PowerShell scripts,
scripts
Friday, 4 July 2008
Show-Queue.ps1
<# .SYNOPSIS Demonstrates a .NET Queue object .DESCRIPTION This script creates a queue, adds some items to it, and displays the queue. Finally, the clear method is called and results displayed. .NOTES File Name : show-queue.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 Tags : system.collections.queue, enque, clear, powershell, scripts .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\show-queue.ps1 $myq object 3 entries in the queue as follows entry 0: Hello entry 1: World entry 2: ! $myq object after the clear: 0 entries in the queue as follows: queue is empty #> ## # Start of script ## # Create new queue object and add three objects to it $myq = new-object system.collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") $myq.Enqueue("!") # Display the properties and values of the Queue "`$myq object" $i=0 "`{0} entries in the queue as follows" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } # Now clear the queue $myq.clear() #show results "";"`$myq object after the clear:" $i=0 "`{0} entries in the queue as follows:" -f $myq.Count if ($myque.count -gt 0) {$myq | % {"entry {0}: {1}" -f $i++,$_ }} else {"queue is empty"} # End of Script
Labels:
clear,
enqueue,
powershell,
PowerShell scripts,
scripts,
system.collections.queue
Get-SortedList.ps1
<# .SYNOPSIS Sorted list sample .DESCRIPTION This script creates then manipulates a sorted list .NOTES File Name : Get-SortedList.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://pshscripts.blogspot.com/2008/07/get-sortedlistps1.html .EXAMPLE PSH [C:\foo]: .\Get-SortedList.ps1 $mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! $mySL after two additions Count: 5 Capacity: 16 Keys and Values: -KEY- -VALUE- AAAA: aaaa First: Hello Second: World Third: ! ZZZZ: zzzz #> ## # Start of script ## # Define Helper Function function PrintKeysAndValues( $myList ) { "`t-KEY-`t-VALUE-" for ( [int] $i = 0; $i -lt $myList.Count; $i++ ) { "`t{0}:`t{1}" -F $myList.GetKey($i), $myList.GetByIndex($i) } "" } # Create and initialise a new SortedList object $mySL = new-object system.collections.SortedList $mySL.Add("First", "Hello") $mySL.Add("Second", "World") $mySL.Add("Third", "!") # Display the properties and values of the SortedList "`$mySL" " Count: {0}" -f $mySL.Count " Capacity: {0}" -f $mySL.Capacity " Keys and Values:" PrintKeysAndValues( $mySL ) # Add two more and display results $mysl.add("AAAA", "aaaa") $mysl.add("ZZZZ" , "zzzz") # display results "`$mySL after two additions" " Count: {0}" -f $mySL.Count " Capacity: {0}" -f $mySL.Capacity " Keys and Values:" PrintKeysAndValues( $mySL ) # End of script
Subscribe to:
Posts (Atom)