SCVMM PowerShell scripts simplify administrative tasks

SCVMM PowerShell scripts simplify administrative tasks

If you think Microsoft Hyper-V PowerShell cmdlets are comprehensive, System Center Virtual Machine Manager (SCVMM) PowerShell scripts offer even greater functionality and options for administrative tasks. These scripts can make a huge difference in the day to day of Hyper-V admins.

SCVMM PowerShell scripts can run on several virtualization platforms, including Hyper-V (R1 and R2), Virtual Server 2005 and VMware. Unlike with Hyper-V PowerShell scripts, which perform tasks directly on hosts, SCVMM acts more like a proxy that can translate PowerShell commands into executables for various hosts.

    Requires Free Membership to View

    When you register, my team of editors will also send you the latest expert resources covering all areas of server virtualization, such as platforms, architectures and strategies, server hardware, managing virtual environments, application issues and more.

    Margie Semilof, Editorial Director

    By submitting your registration information to SearchServerVirtualization.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchServerVirtualization.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

More on PowerShell in Hyper-V environments
How to use PowerShell or VBScript scripting with Hyper-V

Optimizing Hyper-V cluster performance among multiple sites
Because SCVMM PowerShell scripts are compatible with a broad array of systems, its scripting guide is very thorough. Here are some basic cmdlets and scripts that I use frequently.

Get-VM. Both Hyper-V and SCVMM feature the Get-VM cmdlet. But entering Get-VM from the SCVMM command prompt retrieves much more information.

The Get-VM cmdlet returns the attributes of every VM that's managed by SCVMM, which is usually too much information to digest. Here's how to obtain information about a single virtual machine( VM):

Get-VM

Figure 1
(Click image for an enlarged view.)

If you're interested in the VM's name and memory information, enter the following SCVMM PowerShell command:
Get-VM | ft name, memory

Get-VM is a versatile cmdlet that retrieves an enormous amount of information, but you can granularly search for information by attaching attributes to it. Insert "ft" (Format-Table) or "fl" (Format-List), for example, to customize the format of the results.

Set-VM. The Set-VM cmdlet allows administrators to bypass time-intensive graphical user interface (GUI) wizards and change VM settings from the command line. Below are my favorite Set-VM commands:
To set a VM's memory utilization, enter the following command:

Set-VM < SERVERNAME> -memory 2048

Helpful SCVMM PowerShell definitions
Cmdlets: These are the building blocks for commands. Cmdlets perform an action and provide a Microsoft .NET framework object to the next command. All cmdlets follow a verb, noun format (e.g., Get-VM, Get-Help).

Commands: The execution of one or more cmdlets, along with defining parameters that implement specific functions. For more information on SCVMM PowerShell cmdlets and commands, in addition to their differences, visit Microsoft's Cmdlet Overview page
To adjust a VM's CPU count, run this SCVMM PowerShell script:
Set-VM < SERVERNAME> -cpucount 2

The following command designates which users can access a VM through the SCVMM Self-Service Portal. The Self-Service Portal is a stripped-down version of the SCVMM Administration Console, which can grant privileges to users.

Set-VM -owner "DOMAIN\GROUPNAME or USERNAME"

The basic Get-VM and Set-VM cmdlets can simplify many administrative tasks. As you become more familiar with PowerShell, you can create powerful SCVMM scripts that are brief and simple.

Who are you?
Frequently, you'll have bits of information without an identifier for a VM's name. These obscure messages are usually found in the event log. Here are simple SCVMM PowerShell scripts to help identify mysterious VMs.

This script determines a VM's name from its MAC address (e.g., 00:03:ff:07:e7:dc):

Get-VirtualNetworkAdapter -all | where {$_.ethernetaddress -eq "< Insert VM MAC Address>"}

To acquire a VM name from its VM ID (e.g., 1CA3996C-C089-4896-A806-B396BC88F0BB), run this script:

Get-VM | where{$_.VMid -eq ""} | ft name

Unmerged snapshots
Forgotten snapshots and checkpoints can occupy valuable disk space. Often these snapshots were created before an application upgrade but then abandoned. I frequently run, the following script to delete and merge these snapshots before they create management and performance problems.

Here's how to find VMs with snapshots:

Get-VM | where {$_.LastRestoredVMCheckpoint -like "* *"} | ft name, LastRestoredVMCheckpoint, hostname

Which VMs have an ISO image mounted?
I perform this housecleaning command on a regular basis. At times, though, it can also interfere with Quick or Live Migrations.

To find any ISO image that's attached to VMs, enter the following command:

Get-VM | get-virtualdvddrive |Where {$_.iso -like "*.iso"} | ft name, iso --a

Tip: The "-a" (autosize) component at the end of the command adjusts the column widths to prevent the output from truncating.

This command ejects any ISO image attached to VMs:

Get-VM | get-virtualdvddrive |? {$_.iso -like "*.iso"} | set-virtualdvddrive --nomedia

Tip: You can substitute a"?" for "Where."

Here's how to remove specific ISO images from VMs. This command comes in handy when the VM integration components ISO image does not dismount after its installation.

Get-VM | get-virtualdvddrive |? {$_.iso -like "vmg*"} | set-virtualdvddrive --nomedia

Pesky, misconfigured network adapters
When I'm in a rush, I sometimes mistakenly create emulated virtual network adapters instead of the higher-performing synthetic network adapters. This SCVMM PowerShell script identifies virtual network adapter types:

Get-VirtualNetworkAdapter -all | where {$_.virtualnetworkadaptertype -eq "emulated" -and $_.ethernetaddress -like "00:15*"} |ft name,VirtualNetworkAdapterType,ethernetaddress

SCVMM hardware profiles help prevent these administrative mistakes by enabling IT personnel to standardize the VM components throughout a virtual environment. You can apply these hardware profiles during the VM provisioning process with a PowerShell script or GUI wizard.

Clustered VMs
SCVMM PowerShell scripts can also perform any cluster function. This command migrates a VM from one cluster node to another:

Move-VM -VM -VMHost

To set the processor compatibility mode from PowerShell (the guest must be off), enter the following text:

Set-VM -LimitCPUForMigration $True

The beauty of SCVMM is that it's built on PowerShell. As a result, these SCVMM PowerShell scripts provide administrators with in-depth control over multiple virtualization platforms. Learning the PowerShell scripting language can be arduous, but you'll reduce administrative time as you become more familiar with SCVMM scripts.

For more information on Hyper-V and SCVMM scripts, visit my VirtuallyAware blog.

About the expert
Rob McShinsky is a senior systems engineer at Dartmouth Hitchcock Medical Center in Lebanon, N.H., and has more than 12 years of experience in the industry -- including a focus on server virtualization since 2004. He has been closely involved with Microsoft as an early adopter of Hyper-V and System Center Virtual Machine Manager 2008, as well as a customer reference. In addition, he blogs at VirtuallyAware.com, writing tips and documenting experiences with various virtualization products.


This was first published in June 2010

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.