Looking at VM Uptime through WMI

Looking at VM Uptime through WMI

  • Comments 2

Another useful WMI counter that is exposed by Virtual Server is the virtual machine uptime (how long the virtual machine has been running since it was last started or rebooted).  This is reported in seconds, so I had to do some tweaking with mod and div to get it to display nicely, but:

Set vsWMIObj = GetObject("winmgmts:\\.\root\vm\virtualserver")
Set vms = vsWMIObj.ExecQuery("SELECT * FROM VirtualMachine",,48)
For Each vm in vms
    Wscript.Echo "Virtual machine: '" & vm.Name & _
   "'   Uptime: " & vm.Uptime \ 86400 & " days, " & _
   (vm.Uptime \ 3600) mod 24 & " hours, " & _
   (vm.Uptime \ 60) mod 60 & " minutes, " & _
   vm.Uptime mod 60 & " seconds."
Next

Two comments to make, under Virtual Server WMI is only used for reporting statistical counters.  There is no way to change anything through WMI, just observe it.  The second comment is that you should be careful about your slashes :-).  In VBscript '/' will give you a floating point number with appropriate decimal values while '\' performs div and returns the value rounded down to the nearest integer.

Cheers,
Ben

Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • Having recently been criticized (more than that actually) for discussing problems with example code when the code was intended to explain an idea other than the code itself, I'm wondering if I shouldn't post this.  But the code still troubles me so I post this.

    Some decades back, when long distance phone calls weren't cheap, someone got a bill for a phone call roughly 1,443 minutes long.  From looking at the time of day it was obvious what happened.  The phone company's computer had a date rollover in between a program's fetch of the date and a program's fetch of the time of day.  This was a one-in-a-billion occurence so the phone company found it cheaper to fix bills manually than to fix their program.

    With the speed of computers now, one-in-a-billion occurences are next Tuesday, if not the next hour.

    You need to fetch the value of vm.Uptime once, assign it to a local variable, and then do computations on the local variable.

    This makes me wonder whether the problem with keyboard access getting lost in virtual machines might be due to losing track of state changes for exactly the same kind of reason.

  • Hi Norman,

    You are right that I was being lazy - and should have stashed this in a single variable - but as you point out, this is just sample code so I did not spend much time polishing it.

    And no - a snippet of sample code is hardly going to be responsible for timing issues that you see inside the shipping product.

    Cheers,

    Ben

Page 1 of 1 (2 items)