RSS

MS Virtual Server: WMI vs Scripting Object

31 Jan

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-host

for ($i = 1; $i –le ($vms.count); $i++) {
$vm = $vms.Item($i)
$result = SetSecurity($vm)
write-host $vm.name
}

write-host

Advertisements
 
5 Comments

Posted by on January 31, 2007 in Scripts, VBScripts

 

Tags: ,

5 responses to “MS Virtual Server: WMI vs Scripting Object

  1. NoBrainer

    February 17, 2007 at 1:01 pm

    When shutting down VM’s it is advisable to first check if this operation can succeed.

    Use the CanShutdown property of the GuestOS object, like:

    for each objVM in colVMS
    on error resume next
    set objGOS = objVM.GuestOS
    if objGOS.CanShutDown Then
    objGOS.Shutdown()
    Else
    WScript.Echo “Warning: Cannot Shutdown GuestOS!”
    End IF
    on error goto 0
    Next

    There is also an orphaned “End If” in your code sample, at the bottom.

    You may also want to test if the GuestOS object is actually there with “If objGOS is Nothing Then”.

    You could also embelish the code with addl error handling to make it more well behaved.

    George/

     
  2. saltwetfish

    February 19, 2007 at 5:49 pm

    Hi George,

    Thanks for you advise… removed the orphaned end-if.

    The code was only meant to demonstrate how to shutdown Virtual Machines, as such they were not complete. I should have put up some caveats about the codes.

    Thanks again

     
  3. Ben Christian

    April 5, 2007 at 4:08 am

    Thanks, you saved me some searching! I’ve written a few PowerShell scripts for managing VMs for Systems Center Virtual Machine Manager Beta 1 (SCVMM) over at http://www.benchristian.com, but I needed to manage VMs on an individual Virtual Server so your examples have come in handy.

     
  4. Victor

    January 28, 2008 at 8:28 pm

    HI, Thank you very much for your article was excelent , but I need some help… Do you know How I can obtain the name of the real server when I have the Virtual server, please If you or someone know the solution,answer me “vperalta8@gmail.com”, thanks A LOT!!

     
  5. saltwetfish

    January 30, 2008 at 2:38 pm

    Victor,

    Check out this link from Virtual PC Guys: http://blogs.msdn.com/virtual_pc_guy/archive/2005/01/24/359650.aspx

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: