-- Ben Armstrong, Virtualization Program Manager
Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).
Now to get a little trickier - shutting down a virtual machine:
VBScript:
Option Explicit
Dim WMIService
Dim VMList
Dim VMName
Dim VMGuid
Dim ShutdownList
Dim Result
'Specify the name of the virtual machine that I want to shutdown
VMName = "Windows Server 2003"
'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
'Query for the specific virtual machine that I want to shutdown
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "'")
'Get the GUID for the virtual machine I want to shutdown
VMGuid = VMList.ItemIndex(0).Name
'Query for the MSVM_ShutdownComponent that corresponds to the VM GUID that I have
Set ShutdownList = WMIService.ExecQuery("SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" & VMGuid & "'")
'Request a shutdown
Result = ShutdownList.ItemIndex(0).InitiateShutdown(True,"Because I said so")
PowerShell:
#The name of the virtual machine to be shutdown
$VMName = "Windows Server 2003"
#Get the VM Object
$query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $VMName + "'"
$VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
#Get the Shutdown Component for the VM
$query = "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" + $VM.name + "'"
$Shutdown = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
#Request a forced shutdown
$Result = $Shutdown.InitiateShutdown($true,"Because I said so")
Okay, time for some notes:
Cheers, Ben
Thanks a lot Ben :) These scripts will be very handy. I'm already moving some production machines to VM's. Will be using these startup-shutdown-scripts for backup purposes.
I hope you'll be posting more scripts for Hyper-V actions.
Regards,
Wessel