Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
Hi,
I have had the task of having to start and stop virtual machines in Windows Server 2008 Core without the use of Hyper-V Manager or VMM.
It is possible to start and stop the machines if you only have access to the command prompt through the use of WMI.
I have hacked together a script file that can start/stop/list what virtual machines are available and their current state
Instructions
download the file VMControl.vbs (2.77 KB)
run VMControl.vbs with the following arguments
eg. "VMControl.vbs /list"
"/list" = list all VM's and their state"/start virtualMachineName" = starts the the virtual machine"/stop virtualMachineName" = stops the virtual machine"/startAll" = starts all virtual machines on the host providing there is enough resources"/stopAll" = stops all virtual machines on the host"/help" = help information
Here is the code for it
*nb you may have to run this with admin privileges
Option Explicit
'Setup the variables
Dim WMIService
Dim VMList
Dim Hostname
Dim VMName
Dim VMAction
Dim compareType
Dim HOST
Dim VM
Dim VMStatus
Dim AllVMs
'Used to get all machines or just one
compareType = "="
'gets and sets the host name
Set HOST = WScript.CreateObject("WScript.Network")
'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
'Get the arguments from the user input and go to the appropriate sub
if wscript.Arguments.count > 0 then
Select case WScript.Arguments.Item(0)
case "/start"
VMName = WScript.Arguments.Item(1)
VMAction = 2
modifyVM
case "/stop"
VMAction = 3
case "/startAll"
VMName = HOST.ComputerName
compareType = "!="
case "/stopAll"
case "/list"
listVMs
case "?","/?","-?","help","Help","/help","/Help"
helpMsg
case else
end select
else
end if
'Message to display when incorrect input or recived help switch
sub helpMsg
AllVMs = "usage VMControl.vbs"
& VBCr & VBCr & "/start <VM Name> - Starts the Virtual Machine"
& VBCr & "/stop <VM Name> - Stops the Virtual Machine"
& VBCr & "/startAll - Start all Virtual Machines"
& VBCr & "/stopAll - Stop All Virtual Machines"
& VBCr & "/list - List All Virtual Machines"
end sub
'used to turn the VM on or off or saved
sub modifyVM
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem
WHERE ElementName" & compareType & "'" & VMName &"'")
For Each VM In VMList
VM.RequestStateChange(VMAction)
select case VMAction
case 3 VMStatus = "stopped"
case 2 VMStatus = "running"
case 32769 VMStatus = "saved"
AllVMs = VM.ElementName & " is " & VMStatus
next
'used to get and display the VM's
sub listVMs
select case VM.EnabledState
AllVMs = AllVMs + VM.ElementName & " is " & VMStatus & VBCr
'Display a message of what happened or a help message
wscript.echo AllVMs
VMControl.vbs (2.77 KB)
Remember Me