Help with Function Command

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Alisdair Philp (ally.philp_at_dial.pipex.com)
Date: 09/20/04


Date: Mon, 20 Sep 2004 20:07:45 +0100

I'm trying to write a script that connects to servers from a text file read
into a dictionary to get uptime from them. Uptime code is taken from
Microsoft site, but when this runs I get a syntax error at the Function
section. MS code works OK on a PC without the Dictionary code to read in a
servers.txt file. Can anyone help on this?

Thanks in Advance

'Read Server List Text File and Create Dictionary of Server Names
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
      strNextLine = objTextFile.Readline
      objDictionary.Add i, strNextLine
      i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
    Wscript.Echo dtmSystemUptime

Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
         & " " & Mid (dtmBootup, 9, 2) & ":" & _
         Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
         13, 2))
End Function
next



Relevant Pages

  • Re: Scan subnet for Windows servers and return the Names of the servers
    ... Const ForReading = 1 ... fOs = " " ... 'Get Server Name ... Set colItems = objWMIService.ExecQuery("SELECT * FROM ...
    (microsoft.public.scripting.vbscript)
  • Re: Help with Function Command
    ... > 'Read Server List Text File and Create Dictionary of Server Names ... > Set colOperatingSystems = objWMIService.ExecQuery _ ... dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now) ...
    (microsoft.public.scripting.vbscript)
  • RE: VBScript to restart a server
    ... strComputer = "CAM125BW0102" ... Set colOperatingSystems = objWMIService.ExecQuery _ ... For Each objOperatingSystem in colOperatingSystems ... you can make a text file and just read from it with all your server names ...
    (microsoft.public.scripting.vbscript)
  • Select Case?
    ... "server unreachable" and moves on. ... I have included the script below. ... Const ForReading = 1 ... strComputer = objTextFile.ReadLine ...
    (microsoft.public.scripting.vbscript)
  • Win32Shutdown failing with error number 21
    ... I am shutting down a number of servers using the following script. ... The machines are running win2k server or win2k3 server. ... The two which fail are running w2k3 but at least one other that is ... Set colOperatingSystems = objWMIService.ExecQuery _ ...
    (microsoft.public.scripting.vbscript)