Link to home
Start Free TrialLog in
Avatar of Sleestack90
Sleestack90

asked on

VBscript Uptime

I have the following script that displays uptime:

Set objWMIService=GetObject("winmgmts:\\.\root\cimv2")
Set colOperatingSystems=objWMIService.ExecQuery _
  ("Select * From Win32_PerfFormattedData_PerfOS_System")
For Each objOS in colOperatingSystems
  intSystemUptime=Int(objOS.SystemUpTime)
  TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
  Msgbox UpTime(intSystemUptime)
Next
Function UpTime(S)
  M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
  UpTime=D &" Days, " & H MOD 24 &" Hours, " &M &" Minutes"
End Function


I would like to be able to call this inside a command prompt and have the result passed back into the command prompt instead of opening a message box (line 7).
ASKER CERTIFIED SOLUTION
Avatar of madhubabus
madhubabus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sleestack90
Sleestack90

ASKER

When I use this, it does output to the console, however when I call the script in BGinfo this is what it reutrns:

Error evaluating scripted field 'Uptime'
Microsoft VBSctipr runtime error
Line 7, position 2
Object required: 'wscrip'


This is what my script currently looks like:

Set objWMIService=GetObject("winmgmts:\\.\root\cimv2")
Set colOperatingSystems=objWMIService.ExecQuery _
  ("Select * From Win32_PerfFormattedData_PerfOS_System")
For Each objOS in colOperatingSystems
  intSystemUptime=Int(objOS.SystemUpTime)
  TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
  wscript.echo UpTime(intSystemUptime)
Next
Function UpTime(S)
  M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
  UpTime=D &" Days, " & H MOD 24 &" Hours, " &M &" Minutes"
End Function
I am not aware of bginfo, let me check if I can help
This is what needs to be changed: wscript.echo to just echo

Set objWMIService=GetObject("winmgmts:\\.\root\cimv2")
Set colOperatingSystems=objWMIService.ExecQuery _
  ("Select * From Win32_PerfFormattedData_PerfOS_System")
For Each objOS in colOperatingSystems
  intSystemUptime=Int(objOS.SystemUpTime)
  TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
   echo UpTime(intSystemUptime)
Next
Function UpTime(S)
  M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
  UpTime=D &" Days, " & H MOD 24 &" Hours, " &M &" Minutes"
End Function
Put me on the right path