11 Aug 2013

Master virtualization with a new Windows PowerShell cmdlet



In Windows Server 2012, Microsoft has made it possible to fully manage your Hyper-V-based virtualization infrastructure and virtual machines (VMs) from the PowerShell command line. In fact, Windows Server 2012 contains dozens of server virtualization-specific Windows PowerShell v3 commands. Each PowerShell cmdlet may not benefit every data center, but others provide important management capabilities.

The PowerShell v3 syntax may seem confusing at first, but it is well worth learning, especially if you find yourself managing large numbers of virtual machines.

This slideshow outlines five of the most important PowerShell v3 commands every virtualization admin should know.

Get-VM is probably the single most heavily used PowerShell v3 command related to server virtualization. When used without any switches, the Get-VM cmdlet returns a list of all the VMs that are present on the host server. As you can see in the figure, this list also includes information about the VMs' state, their CPU and memory usage, uptime and status. You can also use this command to retrieve granular information about a specific VM through a series of command-line switches.



The Invoke-Command cmdlet performs bulk PowerShell operations against multiple targets. This allows you to more easily manage large numbers of VMs because you can run the cmdlet against multiple Hyper-V hosts. Simply include the –ComputerName switch with the names of the target servers separated by commas and the command that you want to run on the remote servers enclosed in braces.

Suppose for a moment that you want to retrieve the name, state and status of all VMs running on servers named Lab1 and Lab2. You could accomplish this task with the following PowerShell v3 command:

Invoke-Command –ComputerName Lab1, Lab2 {Get-VM | FT VMName, State, PrimaryStatusDescription}

The image shows an example of this command. As you can see, the Invoke-Command directs the Get-VM command at multiple Hyper-V hosts. It also retrieves specific attributes for each VM by specifying those that should be displayed.



The Get-ClusterGroup cmdlet is a part of Windows Server 2012's failover clustering feature, but it is extremely useful in virtual server environments. Most production virtual servers are clustered as a way of improving VM reliability. The Get-ClusterGroup cmdlet reports on the health of clustered VMs.

Running the Get-ClusterGroup cmdlet by itself will retrieve information about all clustered resources, which can include things other than VMs. To limit the list to VMs, you must include the Where GroupType –EQ 'VirtualMachine' option. The full <a href="http://searchservervirtualization.techtarget.com/guides/Get-schooled-in-advanced-Microsoft-PowerShell-v3-cmdlets">PowerShell v3 command looks</a> like this:

Get-ClusterGroup | Where GroupType –EQ 'VirtualMachine' | Get-VM

The image shows the command in action. In the figure, I redirect the output to the Get-VM cmdlet to retrieve a list of clustered VMs; however, you can easily adapt this technique to perform any bulk operation on your clustered VMs.



You can use the Start-VM cmdlet to start any virtual machine. This very simple cmdlet does not require any command-line switches aside from the VM name. Similarly, if you want to stop a VM, use the Stop-VM cmdlet, as shown in the figure. These PowerShell v3 cmdlets come in very handy for performing scripted operations that require a VM to be rebooted.



The New-VM cmdlet creates a brand-new virtual machine from the PowerShell v3 command line. If you use this cmdlet without specifying any attributes other than the name of the VM to be created, then it creates and provisions a VM with the following resources:

    * 512 MB of memory
    * DVD drive
    * 1 virtual processor

You can, of course, script the VM creation process to allocate additional resources such as virtual network adapters and virtual hard drives, but doing so requires additional commands and command-line switches. The figure illustrates how Microsoft gives you the tools to create VMs from the command line.


No comments:

Post a Comment