Shutting down a Hyper-V Virtual Machine

Shutting down a Hyper-V Virtual Machine

  • Comments 1

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:

  • To shutdown the virtual machine you need to get the "ShutdownComponent" for the virtual machine in question and cal the InitiateShutdown method on it.  From a WMI object model point of view the ShutdownComponent is just a device on the virtual machine (much like a VHD or CD).

  • The first thing you do is get the virtual machine's GUID (unique identifier).  You then search over all ShutdownComponents in the system to find the one that matches the virtual machine you want to shutdown.  If you are used to the Virtual Server COM object model this will seem a bit backwards - but you get used to it quickly.

  • InitiateShutdown takes two parameters.  The first is a boolean to indicate whether to perform a forced shutdown or not, the second is a string that will get entered as the reason text in the eventlog inside the virtual machine explaining why the virtual machine was shutdown.

  • InitiateShutdown returns success immediately on delivering the shutdown message.  If you do not do a forced shutdown it can still fail for some reason after this point in time.

Cheers,
Ben

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • 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

Page 1 of 1 (1 items)