• No results found

Cmdlet –erroraction parameter

In addition to the error active preference variable, there is an error action parameter available on cmdlets. This overrides the global error action preference for that specific command. This is useful if you have a single command that may generate an error and you wish to handle that specific command separately.

3. In the powershell console type the following

$erroractionpreference = "Continue”

4. Type the following command which will fail

Get-item fakefile

This above command will generate an error and display this.

5. Type the following to overwrite the error action for the specific command

Get-item fakefile –erroraction “silentlycontinue”

You will note that the above does not display an error as per the previous command did.

Lesson 5 Hands-On : Variables and Type Fundamentals

Objectives

The objectives for this lab are:

 To use different types of variables for various operations

 To create, modify and understand use of an array

 To create, modify and understand use of a hash table

Pre-requisites

The lab requires a Windows 7 client running in a domain environment.

Estimated Time to Complete this Lab

30 minutes

Exercise 1: Working with Variables

Objectives

In this exercise, you will:

 Create variables for use with Numeric and string operations

 Look at the different properties of the variable

Task 1: Log on the VM Environment

Log on to Windows 7 Enterprise client as Contoso\Administrator with the password P@ssword

Task 2: Open Windows PowerShell Session

On the Windows taskbar, click . The PowerShell console opens.

Task 3: Work with Variables

1. type following command:

PS C:\> $x = 10

2. On the next line, type the following command:

PS C:\> $y = 45

You created two variables and assigned a value to it.

3. Now perform an addition operation.

PS C:\> $z = $x + $y

4. The output of this operation will be stored in a third variable, $z.

5. In the PowerShell console, type the following commands to view the output stored in $z and to find the type.

PS C:\> $z 55

PS C:\> $z.GetType()

6. Identify the type of the object.

7. Create another variable $a and assign a string value to it.

PS C:\> $a = "Good Morning,”

8. Create another variable $b and assign another string value.

PS C:\> $b = "How are you?”

9. Create a third variable $c and add variables $a and $b.

PS C:\> $c = $a + $b

10. In the PowerShell console, type the following command to view the output stored in $c:

PS C:\> $c

Good Morning, How are you?

11. Verify the results. This addition operation is also called String Concatenation.

You will now use the variable to store a collection of objects.

12. In the PowerShell console, type the following command to get a list of all running processes on a system:

PS C:\> $Process = Get-Process

13. On the next line, type $Process to view the output.

PS C:\> $Process

Close all instances of notepad except one. If notepad is not running, start it.

14. On the next line, type the following command to find an instance of a running process and store the output in $ProcessNotepad variable:

PS C:\> $ProcessNotepad = Get-Process Notepad

15. Type $ProcessNotepad in the command to view the output.

PS C:\> $ProcessNotepad

16. Type the following commands to view the properties of the object stored in

$ProcessNotepad:

PS C:\> $ProcessNotepad.Threads PS C:\> $ProcessNotepad.CPU PS C:\> $ProcessNotepad.Handles PS C:\> $ProcessNotepad.Path

Exercise 2: Working with Arrays

Objectives

In this exercise, you will:

 Learn how to create arrays and manipulate the values of elements in an array.

Task 1: Log on to VM environment

Log on to Windows 7 Enterprise client as Contoso\Administrator with the password P@ssword

Task 2: Open Windows PowerShell Session

On the Windows taskbar, click . The PowerShell console opens.

Task 3: Create an Array and modify the elements

1. In the PowerShell console, type the following command to create an array with three elements:

PS C:\> $Currency = @("Dollar","Rupee", "Pound")

2. In the PowerShell console, type the following command to access the first element in the array, and view the output:

PS C:\> $Currency[0]

3. In the PowerShell console, type the following command to access the last element in the array, and view the output:

PS C:\> $Currency[-1]

4. In the PowerShell console, type the following command to add a new element to an existing array:

PS C:\> $Currency += "Yen"

5. Type $Currency to verify the addition of element in array.

6. In the PowerShell console, type the following command to change the value of an existing element:

PS C:\> $Currency[2] = "Peso"

7. Type $Currency to verify the change operation in the array.

$Currency

8. In the PowerShell console, type the following command to store the first 10 application event logs:

PS C:\> $logs = get-eventlog application | Select-Object -First 10

9. In the PowerShell console, type the following command to view the properties and methods for the first object of an array:

PS C:\> $logs[0] | Get-Member

10. In the PowerShell console, type the following command to identify the machine name on which the event was generated:

PS C:\> $logs[0].MachineName

11. In the PowerShell console, type the following command to find the description:

PS C:\> $logs[0].Message

Task 4: Combine Two Arrays

1. In the PowerShell console, type the following command to create two arrays:

PS C:\> $Array1 = @(1,2,3,4,5)

PS C:\> $Array2 = @("Six", "Seven", "Eight", "Nine", "Ten")

Make sure that quotes are added for string values or else you may encounter certain errors.

2. In the PowerShell console, type the following command.

PS C:\> $ArrayCombined = $Array1 + $Array2

3. Display the results of $ArrayCombined

$ArrayCombined

Task 5: Create an Array of a Specific Type

1. In the PowerShell console, type the following command to create an array of integer type:

PS C:\> [int[]]$Numbers = @(1,2,3,4,5)

2. Assign a string value to it and notice the behavior.

PS C:\> $Numbers += "Word"

Observe the behavior. It will error!

3. Add a number to the array.

$Numbers += 6

4. Display the contents

$Numbers

Exercise 3: Work with Hash Tables

Objectives

In this exercise, you will:

 Learn how to create a hash table, access and modify the values

Task 1: Log on to VM Environment

Log on to Windows 7 Enterprise client as Contoso\Administrator with the password P@ssword

Task 2: Open Windows PowerShell Session

On the Windows taskbar, click . The PowerShell console opens.

Task 3: Create a Hash Table and work with values.

1. In the PowerShell console, type the following command to create a new hash table:

PS C:\>$NewHashTable = @{"Company"="Microsoft Corporation";"Street"="One Microsoft Way";"City"="Redmond";"State"="WA"}

2. Type $NewHashTable to view the Key-Value pair.

3. In the PowerShell console, type the following command to find the value for Company:

PS C:\> $NewHashTable.Company

4. In the PowerShell console, type the following command to add a new key-value to this table:

PS C:\> $NewHashTable["Telephone"] = "98052-7329"

You added a new key. However, the value added is incorrect. The value is not the telephone number, but the PIN.

5. In the PowerShell console, type the following command to modify and assign the correct value:

PS C:\> $NewHashTable["Telephone"] = "425-882-8080"

6. Type $NewHashTable to verify the correction.

7. In the PowerShell console, type the following command to view the output of the hash table in a table format:

PS C:\> $NewHashTable | FT *

8. In the PowerShell console, type the following command to view the output in a list format:

PS C:\> $NewHashTable | FL *

You can create a hash table to store a specific type of values (integer, string) by prefixing its type before the name. Use the same method for creating arrays of specific type.

9. In the PowerShell console, type the following command to sort the output by value:

PS C:\> $NewHashTable.GetEnumerator() | Sort Value

10. In the PowerShell console, type the following command to sort the output by name:

PS C:\> $NewHashTable.GetEnumerator() | Sort Name

Exercise 4: Work with Hash Tables and Calculated Object Properties

Objectives

In this exercise, you will:

 Learn how to use calculated object properties with hash tables

Task 1: Log on to VM Environment

Log on to Windows 7 Enterprise client as Contoso\Administrator with the password P@ssword

Task 2: Open Windows PowerShell Session

On the Windows taskbar, click . The PowerShell console opens.

Task 3: Working with Hash Tables and Calculated Object properties.

1. Launch two or three instances of Notepad with an interval of 30 seconds for each instance.

2. In the PowerShell console, type the following command to create a hash table and format the output using the Format-Table Cmdlet:

PS C:\> PS C:\> get-process notepad | Format-table

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName --- --- --- --- --- --- -- --- 65 7 2572 7324 79 0.09 1704 notepad 65 7 2568 7068 79 0.06 6896 notepad 65 7 2568 7192 79 0.09 7988 notepad

You can see the key and an associated value to it.

The properties displayed about are present by default for a PowerShell object. You can restrict the output to one or two properties.

3. In the PowerShell console, type the following command to view ProcessName and CPU for instances of Notepad:

PS C:\> get-process notepad | Format-table Name,CPU -AutoSize

Name CPU

4. If you want to find for how long each instance of Notepad is running, you can use a calculated object property to find out the values.

5. In the PowerShell console, type the following command:

PS C:\> get-process notepad | Format-Table ProcessName,CPU,

@{Label="TotalRunningTime";Expression={(get-date) - $_.Start Time}} -autosize

ProcessName CPU TotalRunningTime --- --- --- notepad 0.3900025 00:03:48.5000924 notepad 0.0936006 00:10:02.1069848 notepad 0.0780005 00:11:24.5269387 notepad 0.0936006 00:11:27.2600306

In the first part of the script, you can notice that to create a calculated property a hash table is created using @{}. Later, the expression is specified to provide the value for TotalRunningTime property.

Create a hash table and a calculated object property using the Select-Object Cmdlet.

6. In the PowerShell console, type the following command:

PS C:\> get-process

The list of all running processes on the computer appears.

7. On the next line, type the following command:

PS C:\> get-process | select-object -property ProcessName

The property, ProcessName for all processes on the computer appears.

8. In the PowerShell console, type the following command to use a calculated property:

PS C:\> get-process | select-object -property ProcessName,@{Name="Start Day";

Expression = {$_.StartTime.DayOfWeek}}

You can find on which day the processes were started.

This completes this exercise and lab.

PowerShell for the IT Administrator, Part 1

Lesson 6: PowerShell Scripting

Lesson 6 Demonstration : Scripting

Introduction

The focus for this lesson is scripting in PowerShell. PowerShell is both a command-line shell and a scripting language. It was designed specifically for the IT Administrator and contains a very rich arrangement of language constructs such as variable assignments, arrays, iterations, and flow control. This lesson describes these concepts and introduces the use of functions and profiles.

Objectives

The objectives for this lesson are:

 To define what constitutes a script and basic script elements such as:

 Comments (#)

 White space

 Line terminators (newline, semicolon)

 Script arguments

 To explain how to execute a script and the security features that are applicable to running scripts, including:

 The execution policy and group policy objects

 Script signing

 To explain PowerShell language iteration statements such as:

 Do While and Do Until

 For and ForEach

 While

 To explain PowerShell language flow control statements, such as:

 Break and continue

 Return and Exit

 To explain other statements that are commonly used in scripts, including:

 Labeled statements

 If

 Switch

 To describe functions and their basic features, including:

 Param-blocks and Function-parameter-declaration

 Argument processing

 The [switch] type constraint

 Named blocks (begin, process and end)

 Pipelines and functions

 To explain the concept of profiles in PowerShell:

 Profile types

 Creating and using profiles

 Script libraries and dot sourcing

Prerequisites

A Windows 7 workstation logged onto with administrator credentials.

Estimated time to complete this lesson