Book Image

Learning PowerCLI for VMware VSphere

By : Robert van den Nieuwendijk
Book Image

Learning PowerCLI for VMware VSphere

By: Robert van den Nieuwendijk

Overview of this book

Table of Contents (17 chapters)
Learning PowerCLI
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using arrays and hash tables


In PowerCLI, you can create a list of objects. For example, "red","white","blue" is a list of strings. In PowerShell, a list of terms is called an array. An array can have zero or more objects. You can create an empty array and assign it to a variable:

PowerCLI C:\> $Array = @()

You can fill the array during creation using the following command line:

PowerCLI C:\> $Array = @("red","white")

You can use the += operator to add an element to an array:

PowerCLI C:\> $Array += "blue"
PowerCLI C:\> $Array
red
white
blue

If you want to retrieve a specific element of an array, you can use an index starting with 0 for the first element, 1 for the second element, and so on. If you want to retrieve an element from the tail of the array, you have to use -1 for the last element, -2 for the second to last, and so on. You have to use square brackets around the index number. In the next example, the first element of the array is retrieved using the following command...