Tip: Use Windows PowerShell to Manage Virtual Machines

You can use Windows PowerShell to manage virtual machines running on Hyper-V servers running Server Core, but you must run your scripts remotely from a computer that has Windows PowerShell installed on it. In addition, your Windows PowerShell scripts can be used only to access the WMI interface on the targeted Server Core installation. This means the primary Windows PowerShell cmdlet you will use to manage virtual machines running on Server Core is the Get-WmiObject cmdlet, which also has the associated gwmi alias in Windows PowerShell.

Here are a few examples of how you can use Windows PowerShell scripts to manage virtual machines running on a Server Core installation. Note that these scripts are presented as samples and may need to be customized to work in your environment.

Tips RSS Feed

Subscribe to the TechNet Magazine Tips RSS feed.

Displays the State of All Virtual Machines

$VMState=@{2="Running" ; 3="Stopped" ; 32768="Paused" ; 32769="Suspended";
32270="Starting" ; 32771="Snapshotting" ; 32773="Saving" ; 32774="Stopping" }
get-wmiobject -computername localhost -Namespace root\Virtualization
-query "Select * from MSVM_Computersystem where Description like
'%Virtual%' " | format-table -autosize @{Label=”VM Name”;
expression={$_.elementName}}, Description, @{Label =”VM State”;
expression={$VmState[$_.EnabledState]}}

Creates a Snapshot of All Virtual Machines

$VSMgtSvc=Get-WmiObject -ComputerName localhost
-NameSpace "root\virtualization"
-Class "MsVM_virtualSystemManagementService"
get-wmiobject -computername localhost -Namespace root\Virtualization
-query "Select * from MSVM_Computersystem where Description like
'%Virtual%' " | foreach-object {$VSMgtSvc.psbase.invokeMethod
("CreateVirtualSystemSnapshot",@($_,$Null,$null)) }

Saves the State of All Running Virtual Machines

$VSMgtSvc=Get-WmiObject -ComputerName localhost -NameSpace "root\
virtualization" -Class "MsVM_virtualSystemManagementService"
Get-WmiObject -computername Localhost -NameSpace "root\virtualization"
-Query "Select * From MsVM_ComputerSystem Where Caption Like 'Virtual%'
and EnabledState = 2" | foreach-Object {$_.RequestStateChange(32769) }

From the Microsoft Press book Windows Server 2008 Server Core Administrator’s Pocket Consultant by Mitch Tulloch.

Looking for More Tips?

For more Windows Server tips, visit the TechNet Magazine Windows Server 2008 Tips page.

For more Tips on other products, visit the TechNet Magazine Tips index.