0

I'm generating an "Up-Time" Win7 Gadget and trying to reproduce the .vbs code found in similar Gadgets.

I'm a .js coder.

Relevant JS:

vbStr=GetUpTime();

Relevant VBS:

Function GetUpTime
    Set loc=CreateObject("WbemScripting.SWbemLocator")
    Set svc=loc.ConnectServer(MachineName, "root\cimv2")
    Set oss=svc.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each os in oss
        tim=os.LastBootUpTime
    Next
    GetUpTime=tim
End Function

Essentially this .vbs does the trick, as currently there is only 1 os running. I would like to expand on this by learning:

1) What is the relevance of MachineName?

If I return MachineName instead of tim, I get an undefined value.

2) How to extract individual os's without the For Each loop, equivelant to the .js:

os=oss[n];

3) How to return an array of tim's relative to each os.

The .vbs code loops through the available os's and gets their respective up-times, but the developer only planned for 1 os and as such there was no code to return an array of tim's. After researching .vbs arrays I've found how to create a 'set-length' array, but this is not relevant!

niton
  • 8,771
  • 21
  • 32
  • 52
Lex
  • 11
  • 3

1 Answers1

0
  1. Machine name is undefined so treated as a zero length string. Which means it's ignored. Normally it's the computer on the network that you wish to query. It's optional so it's undeclaredness doesn't raise an error.

  2. Using COM abbreviation (where Items is a default property)

      os=oss(1) 
    

or in full

      os=oss.items(1)
  1. A dictionary is easier (from Help).

     Set d = CreateObject("Scripting.Dictionary")
     d.Add "a", "Athens"   ' Add some keys and items.
     d.Add "b", "Belgrade"
     d.Add "c", "Cairo"
    

NB: JScript uses COM just like VBScript. The code would be similar.

AMagpie
  • 176
  • 1
  • 4