- 论坛徽章:
- 0
|
You can use Windows PowerShell to manage virtual machines running onHyper-V servers running Server Core, but you must run your scriptsremotely from a computer that has Windows PowerShell installed on it.In addition, your Windows PowerShell scripts can be used only to accessthe WMI interface on the targeted Server Core installation. This meansthe primary Windows PowerShell cmdlet you will use to manage virtualmachines running on Server Core is the Get-WmiObject cmdlet, which alsohas the associated gwmi alias in Windows PowerShell.
Here are a few examples of how you can use Windows PowerShell scriptsto manage virtual machines running on a Server Core installation. Notethat these scripts are presented as samples and may need to becustomized to work in your environment.
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) } |
|