Friday 21 November 2008

Display-Array1.ps1

<#
.SYNOPSIS
    Display's an Array    
.DESCRIPTION
    This script displays the values in an arra
.NOTES
    File Name  : Display-Array1.ps1
	Author     : Thomas Lee - tfl@psp.co.uk
	Requires   : PowerShell V2
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PSH [C:\foo]: .\Display-Array1.ps1'
    Initially,
    Integer array:
        1
        2
        3
        4
        5

    Object array:
        26
        27
        28
        29
        30

    After copying the first two elements of the integer array to the Object array,
    Integer array:
        1
        2
        3
        4
        5

    Object array:
        1
        2
        28
        29
        30

    After copying the last two elements of the Object array to the integer array,
    integer array:
        1
        2
        3
        29
        30

    Object array:
        1
        2
        28
        29
        30
#> 

##
#  Start of script
##

# Helper function
Function PrintValues{ 
[Cmdletbinding()]
Param ($myArr )  

ForEach ( $i in $myArr )
  {
    "`t{0}" -f $i
  }
""
}
 
# Start of Sample
# Creates and initializes a new integer array and a new Object array.
$IntArray =  1, 2, 3, 4, 5 
[system.object] $ObjArray = [object] 26, [object] 27, [object] 28,[object] 29,[object] 30

# Prints the initial values of both arrays.
"Initially,"
"Integer array:" 
PrintValues( $IntArray )
"Object array: " 
PrintValues( $ObjArray );

# Copies the first two elements from the integer array to the Object array.
[System.Array]::Copy( $IntArray, $ObjArray, 2)
 
# Prints the values of the modified arrays.
"`nAfter copying the first two elements of the integer array to the Object array," 
"Integer array:" 
PrintValues( $IntArray );
"Object array: " 
PrintValues( $ObjArray );

# Copies the last two elements from the Object array to the integer array.
[System.Array]::Copy( $ObjArray, $ObjArray.GetUpperBound(0) - 1, $IntArray, $IntArray.GetUpperBound(0) - 1, 2 )

# Prints the values of the modified arrays.
"`nAfter copying the last two elements of the Object array to the integer array," 
"integer array:"
PrintValues( $IntArray )
"Object array: "
PrintValues( $ObjArray )

No comments: