Need to do some work on Virtual Machines on Microsoft Virtual Server and realised that I didn’t have a clue on how to manage VS via scripting. I thought that it would be quite easy to find scripting codes, but here is the list of useful scripting pages I’ve found.
- ActiveXperts
- Techtutorials
- Windows SDK
- VMBlog
- Virtual PC Guy from MSDN
- Jose Aguilar
Anyway, I need to find out the virtual machines names for each of the virtual servers we have in the company and what states they are in. I need this, as I need to perform a routine reboot of these servers after a security patch.
My first try was to use WMI, since I did have a hard time looking for a scripting object for virtual servers. I tried out the WMI codes, but found that it only listed virtual machines that are running, can’t trust WMI:
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & _
“\root\vm\virtualserver”)
Set colQuery = objWMIService.ExecQuery _
(“Select * from VirtualMachine”)
For Each objItem in colQuery
WScript.echo objItem.Name
Next
One would think that the code above will list me all virtual machines in the server. Nope, it only lists all virtual machines that are turned on!
A better way is to use scripting object instead:
Set virtualServer = CreateObject(“VirtualServer.Application”)
Set vmCollection = virtualServer.VirtualMachines
For Each vm in vmCollection
WScript.Echo “Name: ” & vm.Name
Next
And I used these codes to shutdown my Virtual servers.
VServerName = MyMSVServer
Set objVS = CreateObject(“VirtualServer.Application”,VServerName)
Set colVMS = objvs.VirtualMachines
c = 0
for each objVM in colVMS
on error resume next
set objGOS = objVM.GuestOS
objGOS.Shutdown()on error goto 0
Next
Incidentally just found out a PowerShell way of listing virtual machines from the Virtual PC Guy:
$vs=new-object –com VirtualServer.Application –Strict
$result = SetSecurity($vs)
$vms = $vs.VirtualMachines
$result = SetSecurity($vms)
write-host
write-host “The following virtual machines are configured:”
write-hostfor ($i = 1; $i –le ($vms.count); $i++) {
$vm = $vms.Item($i)
$result = SetSecurity($vm)
write-host $vm.name
}write-host