Wednesday, 20 August 2008

Get-Int16.ps1

<#
.SYNOPSIS
Illustrates Int16 data type
.DESCRIPTION
This script creates,displays and formats an Int16.
.NOTES
File Name : Get-Int16.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://pshscripts.blogspot.co.uk
.EXAMPLE
PSH [C:\foo]: .\get-int16.ps1
4142
The value of this int16 is: 4142
The value of this int16 is: 4,142

Min value of an Int16 is: -32,768
Max value of an Int16 is: 32,767
#>

##
# Start of Script
##


# Create the int16

[system.int16] $i = 0

# set and display a value:
$i=4142
$i
'The value of this int16 is: {0}' -f $i

# and nicely formatted
'The value of this int16 is: {0}' -f $i.tostring('0,000')
''

# Max/Min value
'Min value of an Int16 is: {0}' -f [System.Int16]::minvalue.tostring('0,000')
'Max value of an Int16 is: {0}' -f [System.Int16]::maxvalue.tostring('0,000')
# End of script

Wednesday, 30 July 2008

Get-AssemblyVersion.ps1

<#
.SYNOPSIS
    Gets the version number for an assembly
.DESCRIPTION
    
.NOTES
    File Name  : Get-AssemblyVersion.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-assemblyversionps1.html
.EXAMPLE
    PSH [C:\foo]: .\Get-AssemblyVersion.ps1'
    Assembly: System.Speech has version number of: 3.0.0.0    
#>

## 
# Start of script
##

# Define the assembly we want to load - a random reference assembly from SDK 3.0
$Pshfile = "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\system.speech.dll"

# Now load the assembly
$Myasm = [System.Reflection.Assembly]::Loadfile($Pshfile)

# Get name, version and display the results
$Aname = $Myasm.GetName()
$Aver =  $Aname.version

# Display results
"Assembly: {0} has version number of: {1}" -f $Aname.name, $aver
# End of script

Tuesday, 29 July 2008

Get-MathPi.ps1

<#
.SYNOPSIS
    Shows use of PI field    
.DESCRIPTION
    This script gets and displays system.math's PI field
.NOTES
    File Name  : Get-MathPI.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PSH [C:\foo]: .\get-mathpi.ps1
    The Value of Pi is 3.14159265358979
#>

##
# Start of script
##

# Obtain value of Pi 
$pi = [math]::pi

# Display the value
"The Value of Pi is {0}" -f $pi
# End of Script

Monday, 28 July 2008

Get-MathE.ps1

<#
.SYNOPSIS
    Illustrates the exponential value 'e'
.DESCRIPTION
    This script uses system.math's E field
.NOTES
    File Name  : get-mathe.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-matheps1.html
.EXAMPLE
    PSH [C:\foo]: . \get-mathe.ps1'
    The Value of E is 2.71828182845905
#>

##
# Start of Script
##
 
# get the value
$e = [math]::e
 
# print it out
"The Value of E is {0}" -f $e
# End of Script

Saturday, 26 July 2008

Get-GuidByteString.ps1

<#
.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
.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

Friday, 25 July 2008

New-Guid.ps1

<#
.SYNOPSIS
    Gets a new guid
.DESCRIPTION
    This script used sytem.guid to create a new guiod
.NOTES
    File Name  : New-Guid
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell 
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PSH [C:\foo]: .\New-Guid.ps1
    Guid
----
8aadd649-9e6b-4f8c-882d-351636ab5ed4
#> ## # Start of Script ## # Create a new GUID $g = [system.guid]::newguid() # Display the new guid $g # End of script

Thursday, 24 July 2008

Get-EmptyGuid.ps1

# Get-Empty Guid.ps1
#
Get's an empty GUID
#
thomas lee - tfl@psp.co.uk

# create an empty GUID
$g = [system.guid]::empty

# Display the empty guid
"The Empty guid: {0}" -f $g



This script produces the following output:



PS C:\Users\tfl> 
E:\PowerShellScriptLib\System.Guid\Get-EmptyGuid.ps1
The Empty guid: 00000000-0000-0000-0000-000000000000


Technorati Tags: ,,

Wednesday, 23 July 2008

Clear-Stack.ps1

<#
.SYNOPSIS
    Sample of clearing a stack using PowerShell    
.DESCRIPTION
    This script creates a script and shows the impact of clearing the stack
.NOTES
    File Name  : clear-stack.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.com/2008/07/clear-stackps1.html
.EXAMPLE
    PSH [C:\foo]: .\clear-stack.ps1
    Initially,
       Count    : 5
       Values:
    jumped
    fox
    brown
    quick
    The
    After Clear,
       Count    : 0
       Values:

#>

##
# Start of script
##

# Create stack
$stack = New-Object system.collections.stack
$Stack.Push( 'The' );
$Stack.Push( 'quick' );
$Stack.Push( 'brown' );
$Stack.Push( 'fox' );
$Stack.Push( 'jumped' );
 
# Display the count and values of the Stack
'Initially,'
'   Count    : {0}' -f $Stack.Count
'   Values:'
$Stack
 
# Clear the Stack
$Stack.Clear()

# Display the count and values of the Stack
'After Clear,'
'   Count    : {0}' -f $Stack.Count
'   Values:'
$Stack

Tuesday, 22 July 2008

Get-HashTable2.ps1

<#
.SYNOPSIS
    A rich example of using hash tables with PowerShell  
.DESCRIPTION
    This script creates, populates a hash table then
    operates on the hash table
.NOTES
    File Name  : Get-HashTable2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-hashtable2ps1_22.html
    http://msdn.microsoft.com/en-gb/netframework/aa663309.aspx
.EXAMPLE
    PS c:\foo> Get-HashTable2.ps1
    The Hash Table so far

    Name                           Value
    ----                           -----
    bmp                            paint.exe
    rtf                            wordpad.exe
    txt                            notepad.exe
    dib                            paint.exe

    Trying to add an entry with an existing key
   ***
   *** An element with Key = "txt" already exists.
   ***
   Continuing

    for key = "rtf", value = wordpad.exe.
    for key = "rtf", value = winword.exe.

    The Hash Table so far
    doc                            winword.exe
    bmp                            paint.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value added for key = "ht": hypertrm.exe

    6 elements in the hash table as follows:
    doc                            winword.exe
    bmp                            paint.exe
    ht                             hypertrm.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value Entries:
    Value = winword.exe
    Value = paint.exe
    Value = hypertrm.exe
    Value = winword.exe
    Value = notepad.exe
    Value = paint.exe

    Key Entries
    Key = doc
    Key = bmp
    Key = ht
    Key = rtf
    Key = txt
    Key = dib
    
    
    Removing("doc")
    Key "doc" is no longer found.
#>

###
#  Start of script
##

# create new hash table
$openWith = @{}
 
# Add some elements to the hash table. There are no 
# duplicate keys, but some of the values are duplicates.
$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 the Hash table
'The Hash Table so far'
$openwith
 
# The Add method throws an exception if the new key is 
# already in the hash table.
  
#first catch exception
 trap [Exception] {
    '***' 
    write-host "*** An element with Key = `"txt`" already exists."
    '***'
    continue
 }
 
# now the offending line.
'';'Trying to add an entry with an existing key'
$openWith.Add('txt', 'winword.exe');
'Continuing';''
 
# The Item property is the default property, so you 
# can omit its name when accessing elements
# A non-existing key comes up empty
"For key = `"rtf`", value = {0}." -f $openWith['rtf']
 
# The default Item property can be used to change the value
# associated with a key.
# add an entry for RTF 
 $openWith['rtf'] = 'winword.exe';
 "For key = `"rtf`", value = {0}." -f $openWith['rtf']
    
# If a key does not exist, setting the default Item property
# for that key adds a new key/value pair.
$openWith['doc'] = 'winword.exe'
 
# Note Progress with this hashtable
'';'The Hash Table so far'
$openwith
''
 
# The ContainsKey method can be used to test keys before inserting them.
# tesst the "ht" key before adding it into the hashtable
if (!$openWith.ContainsKey('ht')) {
            $openWith.Add('ht', 'hypertrm.exe')
            "Value added for key = `"ht`": {0}" -f $openWith['ht']
}
 

# When you use foreach to enumerate hash table elements,
# the elements are retrieved as KeyValuePair objects.
''
 
'{0} elements in the hash table as follows:' -f $openwith.count 
$openwith
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# The elements of the ValueCollection are strongly typed
#with the type that was specified for hash table values.
'';'Value Entries:'
foreach( $s in $valueColl ) {
            'Value = {0}' -f $s
}
 
# To get the keys alone, use the Keys property.
$keyColl = $openWith.Keys
 
# The elements of the KeyCollection are strongly typed
# with the type that was specified for hash table keys.
'';'Key Entries'
foreach( $s in $keyColl ){
        'Key = {0}' -f $s
}
 
# Use the Remove method to remove a key/value pair.
"`nRemoving(`"doc`")"
$openWith.Remove('doc')
# See if it's there
if (!$openWith.ContainsKey('doc')) {
           "Key `"doc`" is no longer found."
 }
# End of script

Monday, 21 July 2008

Get-Stack2.ps1

<#
.SYNOPSIS
    This script shows the use of a stack.
.DESCRIPTION
    This script uses the system.collections.stack
    object.
.NOTES
    File Name  : Get-Stack2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell Version 2.0
.LINK
    This script posted to:
        http://pshscripts.blogspot.co.uk/2008/07/get-stack2.html
.EXAMPLE
    Psh c:\ .\Get-Stack2.ps1
    Stack as start:
    fox
    brown
    quick
    The

    (Pop)		fox
    Stack value after Pop:
    brown
    quick
    The

    (Pop)		brown
    Stack values after 2nd pop:
    quick
    The

    (Peek)		quick
    Stack values after a peek:
    quick
    The  
#>

# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( 'The' )
$myStack.Push( 'quick' )
$myStack.Push( 'brown' )
$myStack.Push( 'fox' )

# Display the Stack
'Stack as start:'
$myStack
''# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
'Stack value after Pop:'
$myStack
''

# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()

# Display the Stack after 2nd pop
'Stack values after 2nd pop:' 
$myStack
''

# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()

# Display the Stack after the peek
'Stack values after a peek:' 
$myStack