As well as being able to qualify data as a specific type, it is possible to also constrain a variable to be a specific type. This is done by using the data type alias on the left hand side of the assignment operator. This means that the variable will only be able to contain data of that specific type. If you try to assign a different type to that variable it will fail.
1. Type the following in the PowerShell console to create a variable
$a = 77
2. Type the following to check the type
$a.gettype()
You will see that you have an int32
3. Type the following create a type constrained variable
[int]$a = “77”
4. Type the following to check the type
$a.gettype()
You will see that you have an int3 even though a string of “77” was assigned . 5. Type the following to assigned a string to the variable
$a = “this will not work”
You will now see that you get an error from PowerShell. This is because the string “this will not work” cannot be converted to an int32. This is because we can only store numbers in the variable due to the type constraint.
6. Type the following to assign another number to the variable
$a=42
This will work as 42 is an int32.
Is it possible to change the constraint on the variable to another type.
7. Type the following to constrain $a to a string
[string]$a = “See I can now be a string”
8. Type the following to check the type
$a.gettype()
You will see that you have a string. If you try to now assign a number this will now fail.
9. Type the following to assign an int32 to $a
$a = 42
You will now see that this succeeds because converting an integer to a string is allowed.
10. You can also effectively remove the constraint from a variable by making it of type object. Type the following to qualify the variable as type object
[object]$a = 42
11. Type the following to check the type
$a.gettype()
You will see that you have an int32.
12. Type the following to assigned a string to the variable
$a = “this works now”
13. Type the following to check the type
$a.gettype()
You will see that you have a string.
Exercise 3: Working with Arrays
Objectives
In this exercise, you will:
Understand the use of an Array in Windows PowerShell.
Learn how to create, modify values in an Array and add Elements in an Array.
Scenario
In the previous exercise, you learnt about the use of variables and the type fundamentals in PowerShell. Let us continue to explore the functionality and use of arrays in this module.
An array object contains a collection of elements of the same type. For example, an array of integers or string.
While evaluating any expression, the output can be either a single object or a collection of objects.
To store the result of all services on a computer, you create an array.
The following is an illustration of a simple one-dimensional array.
Each block is referred to as an element, which stores values.
Arrays can be one-dimensional or multi-dimensional. The following is an illustration of a two-dimensional array.
Task 1: Log on to VM Environment
Log on to the Windows 7 client as Contoso\Administrator.
Task 2: Create Arrays
1. To create an array object, directly assign the element values delimited with a comma to the array variable.
PS C:\> $NewArray = “a”,”b”,”c”
PS C:\> $NewArray.Gettype()
IsPublic IsSerial Name BaseType --- --- ---- --- True True Object[] System.Array
2. We can also create an empty array using the @ operator.
PS C:\>$NewArray = @()
Let us look at creating an array of numbers.
3. In the PowerShell console, type the following command to create an array. Notice the values of elements.
4. Use the Get-Service Cmdlet to get a list of all services running on a computer and store the output in $service as an array.
PS C:\> $service = get-service
PS C:\> $service
Status Name DisplayName --- ---- ---
Stopped AeLookupSvc Application Experience
Stopped ALG Application Layer Gateway Service Stopped AppIDSvc Application Identity
Running Appinfo Application Information Stopped AppMgmt Application Management Stopped aspnet_state ASP.NET State Service
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running AudioSrv Windows AudioPS C:\> $Numbers.gettype()
IsPublic IsSerial Name BaseType --- --- ---- --- True True Object[] System.Array
PS C:\>
Task 3: Add Elements to Arrays
In the example that follows, you will see how you can add elements in an array. Create a variable named $numbers, which will store numbers from 1 to 10.
1. In the PowerShell console, type the following command:
#Create an array and assign values to elements.
PS C:\> $numbers = 1,2,3,4,5,6,7,8,9,10
PS C:\> $numbers.count 10
The count property of array tells us how many elements are present in an Array.
2. To add an element and assign a value, type the following command:
PS C:\> $numbers += 11
The count of the array has increased since you added two elements.
Task 4: Array Addressing
In an array, the first element is referred as index 0.
1 2 3 4 5 6 7 8 9 10
The array holds values for 10 elements. To find the value in the first element, which is 1, refer the index number 0.
In the following example, a variable named $numbers stores the numbers 1 to 10.
Take a count of how many elements you have in the array. Modify the value of the first element in the array.
#Create an array and assign values to elements.
PS C:\> $numbers = @(1,2,3,4,5,6,7,8,9,10)
PS C:\> $numbers.count 10
# Changing the value of 1st element from 1 to 11.
PS C:\> $numbers[0]= 11
7 8 9 10 PS C:\>
In an array, you can use the index number to refer to individual elements.
$a[0] This will display the value of the 1st element.
$a[1] This will display the value of the 2nd element.
$a[-1] This will display the value of the last element in
array.
$a[-2] This will display the value of the second last
element in array.
Exercise 4: Working with Hash Tables
Objectives
In this exercise, you will:
Understand the techniques of creating and accessing different values in Hash table
Learn how Hash tables can be used while building scripts
Scenario
Hash table is a kind of array where we can store the values in a key-value pair.
Hash tables are used when we need to store information in the form of a dictionary array and then access the value of an element using a corresponding label name.
The following is an example of an array representing the ID of employees within an organization:
Dave 183
Roy 478
Alice 671
In this module, the following operations for hash tables will be discussed.
Creating hash tables
Adding to hash tables
Hash table addressing
Task 1: Log on to VM Environment
Log on to the Windows 7 client as Contoso\Administrator.
Task 2: Create Hash Tables
Creating a hash table is similar to creating an array except one difference.
In an array, elements are inside parenthesis (), and in hash tables, elements are inside braces {}.
$Numbers = @{"A"=1; "B"=2; "C"=3}
$Numbers.GetType()
Let us look at a hash table, which contains employee name and ID.
1. In the PowerShell console, type the following command to create a hash table.
$a = @{"Dave"=123; "Roy"=478; "Alice"=671}
$a
Name Value ---- --- Dave 183
Roy 478 Alice 671
Task 3: Add Entries to Hash Tables
In the below example, we are creating a Hash table which will store the information about the computer type.
Note: The order of your keys/value pairs may show up sorted differently than the exercises below, this is normal behavior because hash tables are not ordered.
1. In the PowerShell console, type the following command to create a hash table:
$ComputerType = @{"Type1"="Desktop";"Type2"="Server";"Type3"="Laptop"}
2. In the PowerShell console, type the following command to add a new entry to a hash table:
3. In the PowerShell console, type the following command to add a new entry to a hash table:
4. In the PowerShell console, type the following command to change the value of an existing key:
$ComputerType[“Type4”] =”Netbook”
$ComputerType