Assisted Lab: Implement a PowerShell Script
Scenario
You are working as an IT technician for the managed service provider (MSP) 515support. As an MSP,
515support helps client businesses and institutions install and manage their computer networks. Today,
however, you need to set up a virtualization server with a script to facilitate the creation of virtual
machines (VMs). Technicians and developers often need to create several guest operating systems at a
time so automating this task will improve productivity.
To complete this scenario, develop a Windows PowerShell script to automate this process.
Objectives
This activity is designed to test your understanding of and ability to apply content examples in the
following CompTIA A+ Core 2 objective:
4.8 Identify the basics of scripting.
Configure PowerShell
Start the PowerShell ISE as an Administrator, use the help system, and set the execution policy to
unrestricted.
1. If necessary, select the MS10 VM. Send Ctrl+Alt+Delete and then sign in using the
account Jaime and password Pa$$w0rd
2. From the Start menu, right-click the Windows PowerShell ISE icon select More > Run as
administrator. Confirm the UAC prompt by selecting Yes.
In the Integrated Scripting Environment (ISE), you write your code in the top pane and then use the
prompt in the lower pane to run it or to execute ad hoc PowerShell cmdlets.
Windows PowerShell ISE. (Screenshot used with permission from Microsoft.)
3. At the prompt, type Get-Help and press ENTER. If you are prompted to update the help file,
select No. Read the help file.
4. Run this cmdlet: Get-ExecutionPolicy
The execution policy determines whether scripts need to be signed or not. We need the "Unrestricted"
policy to be applied for this lab.
5. Run the following cmdlet to reconfigure the execution policy: Set-ExecutionPolicy
unrestricted -force
As you can see, cmdlets are the basis of PowerShell. Each cmdlet is composed of a verb-noun phrase and
can take various optional parameters.
The -force switch suppresses confirmation prompts.
Open the script C:\LABFILES\VM1.ps1, and identify the code constructs. Once you understand how the
script works and what it is going to do, run the script.
1. On the Window PowerShell ISE toolbar, select the Open Script button. and browse to
select C:\LABFILES\VM1.ps1. Select Open.
Examining the first version of the VM script. (Screenshot used with permission from Microsoft.)
2.
Look at line 1 in the script. What is the function of the first character?
Define variable
Define line as comment
Define control structure
None of the above
4. Look at lines 2 through 4. These lines declare variables. Each variable name is preceded by $
(this is a PowerShell convention) and is set to an initial value by using the equals sign (=). Each
variable is set to a string value, enclosed in single quotes. Note the color-coding for the variables
and the strings.
5. Look at lines 13 through 17. These five statements contain the code that will execute.
Various cmdlets are used to create a new Hyper-V VM, assign the Windows setup ISO file to its
emulated DVD drive, and then start the VM.
6. In line 13, observe the way the parameters are set and the color coding for the different
values. Note the use of a variable plus a literal string to identify the path for the virtual disk. This
parameter uses double quotes to allow the variable to be substituted during execution.
7. At the prompt, run Get-Help New-VM to see all the parameters that could have been used.
8. In line 16, observe that a parameter is set by using another cmdlet within parentheses.
9. In line 17, observe that the result of the Get-VM cmdlet is piped to the Start-VM cmdlet.
10.
What will be the outcome of running this script?
Start a virtual machine named '$vmname'
Nothing, because the script contains a logic error
Nothing, because the script is all comments
Start a virtual machine named 'VM'
11. Select the Run Script button .
12. From the taskbar, open Hyper-V Manager . Verify that VM is present and running.
13.
Select the Score button to validate this task:
In this hosted lab environment, it is difficult to manipulate the nested VM, so you will not try to proceed
with booting from the product disc.
If you needed to create a large number of VMs, using a script would save a lot of time compared to
completing the New VM wizard over and over.
14. Leave the VM running
Add error handling
Try to run the script again, and see if you can resolve the error.
1. In the ISE window, select the Run button , and observe the error messages.
One of the greatest challenges in developing effective scripts is to anticipate and account for errors.
There are two ways you could approach this one.
o You could change the value of the $vmname variable so that the script creates a
different VM.
o Or you can enable the Try code block to check whether a VM of that name exists
already.
You'll take the second approach for this activity.
2. For each line of code in 6 through 12 and 18, remove the # comment character from the
start of the line. As you uncomment each line, observe how the ISE highlights errors in the
structure of the code. When you uncomment the last line, the errors will be resolved.
3. Save the file and then run it again.
4. Switch to Hyper-V Manager. Verify that the machine uptime has reset. This is a new version
of the VM.
5.
Select the Score button to validate this task:
The Try block is one example of branching code. It is specifically designed to catch an error. In this script,
if Get-VM cannot return a VM object with the name declared by $vmname, the other statements
within Try { … } to stop the VM and remove it are not executed. The statements within the Finally
{ … } block to create the new VM are executed in either case, however
Improve a script with user input
This script might be considered a little bit destructive if you ran it without understanding what it could
do. What if you had spent hours installing Windows and third-party applications to VM and then another
tech ran this script? Linux users might be quite happy with no opportunity to cancel, but Windows users
are accustomed to having the chance to think about whether they really want to run a command. To
accommodate this, you will add a prompt to the script. You will probably need to follow the guided
steps and use the sample provided to accomplish this task.
1. Close the [Link] script and open vm2.ps1.
Examining the second version of the VM script. (Screenshot used with permission from Microsoft.)
2. Position the cursor in line 6, and select the Run Selection button .
3. Select OK at the prompt. Running portions of your code is a useful way to check that what
you have added works.
4. Look at the code in line 6. It defines a variable that gets its value from a system-generated
prompt dialog box. The parameters control the text in the dialog box, the type of buttons, the
icon used, and so on.
5. In the lower prompt window, run write $prompt The value is set to OK.
The value of the variable persists even though you only ran a portion of the code. Its value will stay the
same in this PowerShell session, unless it is changed again by some code.
6. Look at line 7. This version of the script wraps the previous code within an If block. The
code within this If block will run only if the user selects OK at the prompt.
7. Observe the -eq operator used to test the condition. Note also the parentheses used to
enclose the condition.
While many of the code constructs are similar between languages, each language has its own syntax. To
develop code, you need to understand both the general use of code constructs and the syntax of
particular languages.
8. Look at line 21. This bracket closes the If block.
When you start nesting control structures, it becomes very, very easy to make mistakes in the code
syntax. Using different indents for these blocks can help you to keep track of the code structure.
9. Use the Arrow keys to move the cursor between the curly brackets in lines 20 and 21.
Observe how the matching bracket is highlighted in the earlier code (line 21 matches with line 7,
while line 20 matches with line 14).
10. Run the whole script again to test it. At the prompt, select Cancel.
11. Run the script again, selecting OK at the prompt.
12.
Select the Score button to validate this task
Improve a script with control structures
Now, what if you want to create more than one VM? Can you suggest what sort of control structure you
could add to the script to accomplish that?
1.
Which of the following types of control structure will accomplish this goal?
Try block
If block
For block
Any of the above
Complete comprehensive questions
Congratulations! TBC:
Answer the following final comprehensive questions to ensure that you recognize the importance of the
activity steps and the uses for the information you have learned.
1.
Which of the following code constructs was NOT used in this lab?
If block
Do block
Comment
Variable
Try block
2.
Which character must be prefixed to a name to declare a variable in PowerShell?
3.
True or false? The security configuration of the host computer was weakened in order to execute the
scripts demonstrated in this lab.
True
False