Solutions provider takeaway: Windows PowerShell scripts can be used by solutions providers for task automation, which will allow them to let PowerShell take on time-consuming jobs such as consolidating old servers.
Automating common tasks using the Windows Scheduler
IT personnel today spend a lot of time on repetitive tasks to accomplish various jobs. PowerShell provides a powerful language that can be used to write and execute scripts. These scripts can eliminate repetitive tasks and add the necessary logic to complete complex jobs. Since VMM is built entirely on top of PowerShell, anything an administrator can do in the Administrator Console can also be accomplished via PowerShell cmdlets. If you combine that with the ability to integrate with .NET and other data stores that are PowerShell-ready, an administrator should be able to translate a lot of manual work into PowerShell scripts. The ability to schedule PowerShell scripts at specified intervals allows an administrator to do passive management of their system and let PowerShell do some of the heavy lifting during nonworking hours.
Once you have a PowerShell script ready, you may want to execute it at regular intervals and capture its results in a log file. If the cmdlets change data in VMM, you can also view the results in the Administrator Console's jobs view. There are a couple of ways to create a scheduled task in Windows Server. In this section, we will show you how to do this from the Task Scheduler user interface. Optionally, you can use the schtasks.exe utility to create a scheduled task.
To schedule a task from the Task Scheduler, follow these steps:
- Open the Task Scheduler MMCsnap-in. Task Scheduler is located in either Control Panel\System and Security\Administrative Tools\Task Scheduler or Control Panel\Administrative Tools\Task Scheduler, depending on the version of Windows installed.
- Select Create Task.
- Enter a Task Name like
- Select Run Whether User Is Logged On Or Not and chose to store the password.
- Select Change User Or Group to enter a user that has the proper VMM privileges to execute this PowerShell script.
- In the Triggers tab, enter the schedule you would like to create for this scheduled task. For example, you can choose to run this script daily at 8 p.m.
- In the Actions tab, as shown in Figure 8.13, add a new action and select Start A Program. In the program path, enter
- This is the full path to Windows PowerShell 1.0.
Windows PowerShell automated script. D:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
For arguments, enter the following:
-PSConsoleFile "D:\Program Files\Microsoft System Center Virtual Machine Manager 2008 R2\bin\cli.psc1" -Command " & '\\hypervhost1.vmmdomain.com\ MSSCVMMLibrary\Scripts\GetVMStatus.ps1'"
If you were to execute this command from a regular command window, it would look like this:
D:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "D:\Program Files\Microsoft System Center Virtual Machine Manager 2008 R2\bin\cli.psc1" -Command " & '\\hypervhost1.vmmdomain.com\MSSCVMMLibrary\Scripts\GetVMStatus.ps1'" -NoProfile -Noninteractive
- Click OK and enter the password for the account that will execute the scheduled task.
- From the Task Scheduler MMC, you can view all your scheduled tasks, check for their last run time, and see if there were any errors in execution based on the last run result.
Sometimes, it is easier to check if a scheduled task is executing by looking at a log file. The following sample PowerShell script shows you how to log that information to a file:
Write-Output "Script executing at " (date)
# Get a connection to the local VMM server
$c = get-vmmserver localhost
# Make a sample query to get some data from VMM
$results = get-vm | select name, status, vmid, ID, hostname
# Create a log file in the temp directory
$filepath = "$env:temp\PSscriptOutput.log"
# Append to the log file the current time and the data retrieved from VMM
Add-Content (date) -Path $filepath
Add-Content $results -Path $filepath
Add-Content "-------------" -Path $filepath
Because scheduled PowerShell scripts don't offer the same degree of debugging, you need to ensure that the proper execution policies are in place for PowerShell scripts. It is recommended that all scripts you execute using the Task Scheduler are signed using a code signing certificate issued by a certificate authority. This will enable you to set the PowerShell execution policy to a more secure level like the AllSigned option. After you sign a script using the Set-AuthenticodeSignature cmdlet, you will need to add the publisher of the script to your trusted publishers. PowerShell will prompt you to do that on the first execution of the script.
About the book
This chapter excerpt on Automation Using PowerShell is taken from the book Mastering Virtual Machine Manager 2008 R2. This book offers information on the features, capabilities and architecture that solutions providers need for a successful Virtual Machine Manager (VMM) 2008 R2 deployment. You will find out the best ways to accomplish tasks such as deploying virtual machines to Hyper-V hosts or backing up and planning for recovery of the VMM server.
In Chapter 9, ''Writing a PRO Pack,'' we will cover the PRO feature of VMM. PRO allows an administrator to execute a PowerShell script or perform a VMM action based on a set of alerts detected by System Center Operations Manager (OpsMgr). OpsMgr is a comprehensive data center monitoring tool. In this case, a PowerShell script is executed in response to a dynamic event.
Windows PowerShell examples
In the following sections, we will list a few different Windows PowerShell scripts that use the VMM cmdlets to accomplish important tasks in VMM, make it easier for an administrator to execute repetitive actions, and allow an administrator to get quick status on the health of VMM objects.
Creating virtual machines
There are many ways to create a virtual machine in VMM. In this section, we will take a look at an example of how to create a highly available (HA) VM and find the best suitable host on which to place it. The best suitable host is found by the Intelligent Placement feature of VMM based on the properties of the VM and the available hosts. Listing 8.2 contains the code for creating the HA virtual machine.
Listing 8.2: Creating a new highly available virtual machine
# we have multiple results, so pick the top one
$targethost = $orderedrating.VMhost
Write-Warning "There is no suitable host for this VM's profile"
Write-Warning "There is no suitable cluster disk to place this VM on"
For more information on this topic, check out our features on SCVMM PowerShell commands, PowerShell JEA and PowerShell malware.