Module 2 PowerShell v4 Lab
Module 2 PowerShell v4 Lab
Version 1.4
Microsoft Confidential
Conditions and Terms of Use
Microsoft Confidential - For Internal Use Only
This training package is proprietary and confidential, and is intended only for uses described in the training materials. Content
and software is provided to you under a Non-Disclosure Agreement and cannot be distributed. Copying or disclosing all or any
portion of the content and/or software included in such packages is strictly prohibited.
The contents of this package are for informational and training purposes only and are provided "as is" without warranty of any
kind, whether express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular
purpose, and non-infringement.
Training package content, including URLs and other Internet Web site references, is subject to change without notice. Because
Microsoft must respond to changing market conditions, the content should not be interpreted to be a commitment on the part
of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication. Unless
otherwise noted, the companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events
depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.
Microsoft Confidential
Copyright and Trademarks
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject
matter in this document. Except as expressly provided in written license agreement from Microsoft, the furnishing of this
document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means
(electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.
Microsoft®, Internet Explorer®, and Windows® are either registered trademarks or trademarks of Microsoft Corporation in the
United States and/or other countries. Other Microsoft products mentioned herein may be either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are property of their
respective owners.
Microsoft Confidential
PowerShell v4 Introduction 5
Contents
MODULE 2: POWERSHELL V4 ................................................................................................................... 1
CONTENTS .................................................................................................................................................... 5
LAB 1: POWERSHELL V4 INTRODUCTION ............................................................................................. 6
EXERCISE 1: POWERSHELL BASICS .................................................................................................................. 8
Task 1: Review the available help within Windows PowerShell ...................................................... 8
Task 2: List the commands available in Windows PowerShell........................................................10
Task 3: Format and filter output using Windows PowerShell .........................................................11
EXERCISE 2: INTRODUCING WINDOWS POWERSHELL ISE ...............................................................................13
Task 1: Exploring the Windows PowerShell ISE ................................................................................13
Task 2: Running a script file ...............................................................................................................17
LAB 2: ADVANCED POWERSHELL V4 ................................................................................................... 18
EXERCISE 1: DESIRED STATE CONFIGURATION................................................................................................19
Task 1: Change the Computer Name and IP Address using DSC ...................................................19
Task 2: Promote the first DC in the Contoso Domain using DSC ...................................................23
Task 3: Join WINSRV-WEB1 to the Contoso Domain ......................................................................25
EXERCISE 2: USING POWERSHELL WORKFLOWS .............................................................................................28
Task 1: Prepare WINSRV-2008 Server...............................................................................................28
Task 2: Installing Windows Management Framework 4.0 (PowerShell v4) ...................................29
Task 3: Promote WINSRV-2008 as a Domain Controller ................................................................30
EXERCISE 3: USING POWERSHELL WEB ACCESS .............................................................................................31
Task 1: Installing PowerShell Web Access using PowerShell ..........................................................31
Microsoft Confidential
6 PowerShell v4 Introduction
Objectives
After completing this lab, you will be able to:
Use Windows PowerShell Help features.
List all PowerShell Cmdlets.
Explore the PowerShell Integrated Scripting Environment (ISE).
Create and execute a PowerShell script.
Prerequisites
Student Logged into their “Host Machine” (This is the server that you logged in to
for example : SVVMASP## Server)
WINSRV-AD1, WINSERV-WEB1, and WINSRV-2008 are running, (This will be
completed in the lab)
Scenario
There is a requirement to provide information about the hotfixes and software updates
installed for all of your servers. You need to find the software update ID number, install
date, and the support webpage. The deadline for the task is next week and is required
monthly. We have decided that PowerShell is the best option. In order to complete the
task we need to explore the PowerShell features.
Microsoft Confidential
PowerShell v4 Introduction 7
Contoso.com Domain
SVVMASP##
RDS Gateway
Your Login
From your location
Msftonlinerepro\loginID
Microsoft Confidential
8 PowerShell v4 Introduction
8. Enter the following at the Windows PowerShell command prompt to see a list of
available help topics:
Microsoft Confidential
PowerShell v4 Introduction 9
Help *
Be sure to place a space between Help and *
The command will fill an entire screen and then pause. Press the Enter key to
show the next output line, or press the Space Bar key to advance to the next
page.
9. Press the space bar until the command prompt returns. Alternatively, you can
type the letter ‘Q’ to cancel the output.
10. Enter the following command to see a graphical list of all cmdlets available. Note
that this command is NOT available in Server Core:
Show-Command
11. Type “Get-Process” in the Name field of the “Commands” window and select
“Get-Proccess”, then click “Run” to display all the processes running on your
machine.
12. Enter the following at the Windows PowerShell command prompt to view help
information about the Get-Command cmdlet:
Help Get-Command
The help contains the syntax for the command as well as a brief description
13. Enter the following at the Windows PowerShell command prompt to see detailed
help for the Get-Command cmdlet:
Help Get-Command -Detailed
The output includes details about the parameters for the cmdlet, as well as
some examples.
Note: Every command has three different levels of help available:
The default view shows the command description and syntax.
The detailed view adds usage examples and complete documentation.
The full view adds command’s technical details including parameter
and return value data types.
14. Update the Powerhe following command:
Update-Help -SourcePath “C:\Labfiles\PSHelp”
Microsoft Confidential
10 PowerShell v4 Introduction
If the server has Internet access, the command is Update-Help. To download the
Note: help files for servers without Internet access, use this command: Save-Help -
DestinationPath “PathGetG”. The Update-Help command may error on the ISE
Modules but the Get-Help command should work as expected.
15. Hit the up arrow twice to come back to "Help Get-Command" and press Enter to
review the difference after the Help is updated.
Microsoft Confidential
PowerShell v4 Introduction 11
2. Enter the following and press enter to view the status of the Spooler service:
Get-Service -Name Spooler
3. Enter the following and press enter to view a list of all services that begin with M.
Get-Service M*
The status of all the services beginning with M display.
4. Enter the following and press enter to see the same list of services. This time, the
output is in list format.
Get-Srvice M* | Format-List
In this example there are two commands separated by a pipe (|) character. This
Note: means that the output of the first command is sent as the second command’s input.
5. Enter the following to see the same output, this time shown in a custom format:
Get-Service M* | Format-Custom
The status of all services beginning with M is shown in the custom format
6. Enter the following to view a list of all services ordered by their status:
Get-Service | Sort-Object Status
7. Enter the following to view a list of services that the Spooler service requires to
start.
Get-Service Spooler | Select-Object ServicesDependedOn
A list of services that the spooler service depends on is shown.
Microsoft Confidential
12 PowerShell v4 Introduction
Microsoft Confidential
PowerShell v4 Introduction 13
2. In the Windows PowerShell ISE window click View | Show Script Pane
Microsoft Confidential
14 PowerShell v4 Introduction
4. If you click (or navigate via keyboard) to Get-EventLog for example, a tooltip
displays the syntax of the cmdlet.
Microsoft Confidential
PowerShell v4 Introduction 15
5. Type the following line in the script pane of Windows PowerShell ISE:
$InstalledHotfix = Get-HotFix
This line creates a variable called $InstalledHotfix. It then finds all of the Hotfixes
Note:
installed on the computer and stores them in the variable.
6. In the next line, use the Snippets feature by right clicking in the script pane and
choose Start Snippets.
7. In the pop up window, double click the foreach option.
9.
10. Run the script by clicking the green triangle in the toolbar
Microsoft Confidential
16 PowerShell v4 Introduction
This puts actual data into the variables. Once this occurs, we can choose
properties that are stored in the HotFix object more easily.
11. Between the two { } type the following commands:
$HotFixIDNumber = $Hotfix.
Notice the popup that allows you to choose the property of the HotFix object. Choose HotFixID.
Microsoft Confidential
PowerShell v4 Introduction 17
Script files store PowerShell commands in a file, providing an easy way to run a list of
commands. You only need to tell PowerShell to run the script file. A little knowledge on
execution policies is necessary for you to understand the reasons behind the security
features of Windows PowerShell. The execution policy enables you to determine which
Windows PowerShell scripts (if any) can execute on your computer. Windows PowerShell
has four different execution policies:
Restricted - No scripts are allowed to run. Windows PowerShell can only be used in
interactive mode.
AllSigned - Only scripts signed by a trusted publisher can be run.
RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they
can be run.
Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
1. Open the standard PowerShell window by clicking the icon near the start button.
2. Type the following command:
Get-ExecutionPolicy
The default in Windows Server 2012 R2 is remote signed.
3. Execute the script saved in previous exercise by navigating to Documents folder
(cd Documents) and running the script (.\Get-InstalledUpdates.ps1)
Unlike command line scripts, to launch powershell scripts located in the current folder
a .\ prefix is required (indicating script is located in current folder)
4. The script will run and display all updates installed on the server.
5. This script illustrated the PowerShell ISE scripting environment. An easier way to
pull the same data is below.
This command gets all of the hotfixes and selects the properties we are interested in,
then it formats the results in a table (ft) with each column autosized appropriately.
Microsoft Confidential
18 PowerShell v4 Introduction
Objectives
After completing this lab, you will be able to:
Understand and create PowerShell DSC scripts
Install PowerShell v4 on Windows Server 2008 R2
Understand and create PowerShell Workflow scripts
Install and use PowerShell Web Access
Prerequisites
Student Logged into their “Host Machine” or SVVMASP## Server
WINSRV-AD1, WINSERV-WEB1, and WINSRV-2008 are running,
Scenario
You are required to build a repeatable process to deploy a new Windows Server 2012 R2
Forest. This process needs to verify that all settings are correct. If not, the process will
need to fix all settings that are out of compliance.
Microsoft Confidential
PowerShell v4 Introduction 19
5. The PowerShell ISE window will open. In the bottom blue command window type
Hostname and press enter.
Microsoft Confidential
20 PowerShell v4 Introduction
Notice the current Name and IP Address of the computer. We will be running a
DSC script that will change the name to WINSRV-AD1 and the IP address to
192.168.1.103
7. Review the script in the script view pane. The first line of the script is very important.
It starts the DSC configuration settings by using a new keyword in PowerShell v4,
called Configuration. It is followed by the name of the configuration. In our script
it is named ChangeComputerNameAndIP
8. The following lines are required to import the DSC resource kit modules.
The next section uses the Node keyword to determine which computer(s) will apply the
changes, in this case it is the localhost.
9. These two sections are assigning the IP address for the computer and configuring
the DNS server.
Microsoft Confidential
PowerShell v4 Introduction 21
The boxes shaded with blue indicate the resource that will be performing the
action. The boxes shaded in red are friendly type names. These names can be
anything the Admin choses to describe the desired setting.
10. The next section will change the name of the computer to WINSRV-AD1.
11. We are done with the changes that we would like to make using this DSC script,
so we need to close the Node section and the configuration section with two
Brackets " } "
12. MOF files are used test or modify the settings on the computer. Create these files
by calling the name of the Configuration at the top of the script. The files are
stored in a folder with the same name.
13. Finally, we will call the DSC script to do the testing and changes.
Microsoft Confidential
22 PowerShell v4 Introduction
14. After looking through the script, and when you are ready to run it, click the green
triangle at the top of the PowerShell ISE screen.
15. View the progress bar and the steps the script is taking in the bottom command
window.
Notice the warning that the computer name change will only take effect after the
required reboot.
16. After the computer finishes the reboot, logon as Administrator password:
LS1setup!
Microsoft Confidential
PowerShell v4 Introduction 23
In some cases, the IP Address does not change. This is because the VM is
Important: defaulting to “Ethernet 2” instead of “Ethernet”, which the script is designed to seek.
The Ipconfig result for this error would look like this:
IF for some reason your IP Address has not changed, Repeat steps 2-6. You
only need to do this if you have this specific issue. Otherwise, continue to
Task 2.
In the script view, change the following two lines of script to reflect your
VMs InterfaceAlias. Click Save.
b. The first Prompt is for the Safe Mode Admin Password. Type the following
and click OK:
User Name: Administrator
Password: LS1setup!
Microsoft Confidential
24 PowerShell v4 Introduction
c. The next prompt is for the Domain Admin credentials. We do not need
these when we are building the first DC, only when we are adding DCs to
a domain or verifying the domain settings are correct. Type the following
and click OK:
User Name: contoso\administrator
Password: LS1setup!
Microsoft Confidential
PowerShell v4 Introduction 25
4. The script should begin to install the Cntoso forest. When it completes, scroll
back up the screen to look at the steps performed to promote this server to a
Domain Controller. Notice the warning about the server requiring a reboot, type:
Restart-Computer –force
After reboot, the system configuration before log-in take a few moments.
Microsoft Confidential
26 PowerShell v4 Introduction
8. When the server reboots, choose other user and logon with the following
credentials:
Username: contoso\administrator
Password: LS1setup!
In some cases, the IP Address does not change. This is because the VM is defaulting
Important: to “Ethernet 2” instead of “Ethernet”, which the script is designed to seek.
The Ipconfig results for the error will look like this:
Microsoft Confidential
PowerShell v4 Introduction 27
IPAddress NewIPAddress
IPAddress = 192.168.1.220
InterfaceAlias = “Ethernet 2”
Subnetmask = 24
AddressFamily = “IPV4”
DnsServerAddress DnsServerAddress
Address = “192.168.1.103”
InterfaceAlias = “Ethernet 2”
AddressFamily = “IPV4”
Microsoft Confidential
28 PowerShell v4 Introduction
Microsoft Confidential
PowerShell v4 Introduction 29
Microsoft Confidential
30 PowerShell v4 Introduction
Username: administrator
Password: LS1setup!
9. Open Windows PowerShell.
10. Verify that PowerShell v4 is installed, type: $PSVersionTable
PSVersion should be 4.0
If Tasks 1 and 2 are not completed successfully, you will not be able to complete the
Important: Workflow. Do not skip these steps.
Be sure you are on WINSRV-AD1 or the next task items will not work.
Important:
Microsoft Confidential
PowerShell v4 Introduction 31
Scenario
You need to deploy a management tool that allows any device access to manage
the server environment.
Prerequisites
Make sure that WINSRV-AD1, WINSRV-WEB1, and WINSRV-2008 are running.
Microsoft Confidential
32 PowerShell v4 Introduction
6. Switch servers to WINSRV-AD1 and log in if you are not already, Open Server
manager and In the Server Manager Dashboard, click on Local Server on the left
side.
7. In the Server Properties for the Local Server, you will see the option for IE
Enhanced Security Configuration. Click ‘On’ to change the option. If
Administrators is already ‘Off’, skip step 8.
8. A prompt with the options to turn off Internet Explorer Enhanced Security
Configuration for Administrators and/or Users. Turn Off for Administrators and
Click OK.
9. Open Internet Explorer and browse to https://winsrv-web1/pswa. As we are
using a Test Certificate, you will see a warning. Click on Continue to this website
(not recommended). If you see a security warning, click ok.
10. Once you click on Continue you will see a Windows PowerShell Web Access
login
Microsoft Confidential
PowerShell v4 Introduction 33
Microsoft Confidential