• No results found

Lab B: Moving from Script to Function to Module

In document TrainerHandbook (Page 186-190)

Scenario

You have written a script that performs an administrative task in your environment. You have to package that script as a Windows PowerShell script module so that it can be more easily used by other

administrators in your environment.

Objectives

After completing this lab, students will be able to:

 Convert a script to a function

 Convert a script to a script module

 Debug a script by using basic inline breakpoints

Lab Setup

Estimated Time: 30 minutes

Virtual Machines: 10961B-LON-DC1, 10961B-LON-CL1 User Name: ADATUM\Administrator

Password: Pa$$w0rd

The changes that you make during this lab will be lost if you revert your virtual machines at another time during class.

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must follow these steps:

1. On the host computer, move the pointer over the bottom left corner of the taskbar, click Start, and then click Hyper-V Manager on the Start screen.

2. In Hyper-V® Manager, click 10961B-LON-DC1, and in the Actions pane, click Start.

3. In the Actions pane, click Connect. Wait until the virtual machine starts.

4. Sign in by using the following credentials:

o User name: Administrator o Password: Pa$$w0rd o Domain: ADATUM

5. Repeat steps 2 through 4 for 10961B-LON-CL1.

6. Perform the lab steps on the 10961B-LON-CL1 virtual machine.

Exercise 1: Convert the Script to a Function

Scenario

You have written the following script:

<#

.SYNOPSIS

Retrieves disk space information.

.DESCRIPTION

Retrieves disk information from a single computer.

MCT USE ONL Y. STUDENT USE PROHIBITED

10961B: Automating Administration with Windows PowerShell 08-17

.PARAMETER ComputerName

The name of the computer to query.

.PARAMETER DriveType

The type of drive to query. Defaults to 3, representing local fixed disks.

.EXAMPLE

.\Get-DiskInfo -ComputerName localhost -Verbose

#>

[CmdletBinding()]

Param(

[Parameter(Mandatory=$True)][string]$ComputerName, [int]$DriveType = 3

)

Write-Verbose "Getting drive types of $DriveType from $ComputerName"

Get-WMIObject -Class Win32_LogicalDisk -Filter "DriveType=$DriveType" ` -ComputerName $ComputerName |

You now want to convert that script to a function, in preparation for packaging the script as a script module.

The main tasks for this exercise are as follows:

1. Add the function declaration 2. Execute the function

3. Test the script

 Task 1: Add the function declaration

1. In the Windows PowerShell ISE, open E:\Mod08\Labfiles\LabB\Exercise1-Task1.ps1.

2. Wrap all the contents of the script in a function named Get-DiskInfo.

3. Save the script as C:\Tools.ps1.

 Task 2: Execute the function

1. In your Tools.ps1 script, add the following to the end of the script:

Get-DiskInfo –Comp localhost

This command will execute the function when you run the script.

2. Save the script.

 Task 3: Test the script

Run your Tools.ps1 script.

Results: After completing this exercise, you will have converted the code in your script into a function.

Exercise 2: Save the Script as a Script Module

Scenario

You have written the following script:

MCT USE ONL Y. STUDENT USE PROHIBITED

08-18 Moving from Command to Script to Module

function Get-DiskInfo { <#

.SYNOPSIS

Retrieves disk space information.

.DESCRIPTION

Retrieves disk information from a single computer.

.PARAMETER ComputerName

The name of the computer to query.

.PARAMETER DriveType

The type of drive to query. Defaults to 3, representing local fixed disks.

.EXAMPLE

.\Get-DiskInfo -ComputerName localhost -Verbose #>

[CmdletBinding()]

Param(

[Parameter(Mandatory=$True)][string]$ComputerName, [int]$DriveType = 3

)

Write-Verbose "Getting drive types of $DriveType from $ComputerName"

Get-WMIObject -Class Win32_LogicalDisk -Filter "DriveType=$DriveType" ` -ComputerName $ComputerName |

Select-Object -Property @{n='DriveLetter';e={$PSItem.DeviceID}},

@{n='FreeSpace(MB)';e={"{0:N2}" -f ($PSItem.FreeSpace /

You have to package this script as a Windows PowerShell script module.

The main tasks for this exercise are as follows:

1. Remove the function call

2. Save the script as a script module 3. Test the script module

 Task 1: Remove the function call

1. Continuing on from the last exercise in your C:\Tools.ps1 script, remove the line that runs the Get-DiskInfo function because it is no longer needed. This should be the last line in your script.

2. Save the script.

 Task 2: Save the script as a script module

Save your Tools.ps1 script as a script module named MyTools.

 Task 3: Test the script module

1. In the Windows PowerShell ISE, switch to the Console pane.

2. Run the Get-DiskInfo command. Specify localhost as the computer name.

3. Remove the MyTools module from memory.

Results: After completing this exercise, you will have saved your script as a script module.

MCT USE ONL Y. STUDENT USE PROHIBITED

10961B: Automating Administration with Windows PowerShell 08-19

Exercise 3: Add Debugging Breakpoints

Scenario

You have created a script module named MyTools. In this exercise, you will add debugging breakpoints to the script module.

The main tasks for this exercise are as follows:

1. Add a breakpoint 2. Test the script module

 Task 1: Add a breakpoint

1. Continuing on from the previous exercise in your MyTools.psm1 script, add a debugging breakpoint on the line after the Write-Verbose command. The debug output should include the computer name that the function is about to query.

2. Save the script module.

 Task 2: Test the script module

1. In the Windows PowerShell ISE, switch to the Console pane.

2. Run the you Get-DiskInfo command. Specify localhost as the computer name, and do not enable debugging.

3. Run the Get-DiskInfo command. Specify localhost as the computer name, and enable debugging.

4. At the debug prompt, suspend the script.

5. Display the contents of $ComputerName.

6. Exit debug mode.

7. Let the command continue.

8. Remove the MyTools module from memory.

Results: After completing this exercise, you will have added debugging breakpoints to the MyTools script module.

Question: If you have previous experience in scripting or programming, does Write-Debug work like other debuggers you have used?

MCT USE ONL Y. STUDENT USE PROHIBITED

08-20 Moving from Command to Script to Module

Lesson 3

In document TrainerHandbook (Page 186-190)

Related documents