Option Explicit Dim sService Dim sHyperVInstance Dim sName Dim sOption Dim sGUID Dim sShutdown 'This script should be called from cscript.exe 'Like this: ' 'c:\windows\system32\cscript.exe HManage.exe [name] [action] ' ' [name] - Name of the virtual machine ' [action] - Action to take. Possible options: ' stop - stop the vm ' start - start the vm ' forcestop - force shutdown of vm (Possible data loss) ' 'Make sure we get a name and an action if (WScript.Arguments.Count<2) then WScript.Echo("Missing arguments") WScript.Echo("Expecting: [name] [action]") WScript.Echo("") WScript.Echo("Possible Actions: stop, start, forcestop") WScript.Quit end if 'Get the name of our VM sName = WScript.Arguments.Item(0) 'Get our option sOption = Trim(UCase(WScript.Arguments.Item(1))) 'Get the WMI object for virtualization Set sService = GetObject("winmgmts:\\.\root\virtualization") 'Get our pointer to the vm within its list of vms Set sHyperVInstance = sService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & sName & "'") 'Check to make sure the name is valid if (sHyperVInstance.Count=0) then WScript.Echo("Could not find the Virtual Machine named: " & sName) WScript.Quit end if 'Process our action option if (sOption="START") then sHyperVInstance.ItemIndex(0).RequestStateChange(2) WScript.Echo("Started " & sName) elseif (sOption="STOP") then sGUID = sHyperVInstance.ItemIndex(0).Name Set sShutdown = sService.ExecQuery("SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" & sGUID & "'") sShutdown.ItemIndex(0).InitiateShutdown True,"Scripted shutdown" WScript.Echo("Shutdown " & sName) elseif (sOption="FORCESTOP") then sHyperVInstance.ItemIndex(0).RequestStateChange(3) WScript.Echo("Force shutdown of " & sName) else WScript.Echo("Unknown action: " & sOption) WScript.Echo("") WScript.Echo("Possible actions: STOP, START, FORCESTOP") end if