?
Solved

I want a script to get all restarted dates and time.Is there a way to retrieve such data remotely.

Posted on 2007-07-27
9
Medium Priority
?
391 Views
Last Modified: 2012-05-05
Hi,

I want a script to get all restarted dates and time.Is there a way to retrieve such data remotely.

Regards
Sharath
0
Comment
Question by:bsharath
  • 5
  • 4
9 Comments
 
LVL 19

Expert Comment

by:weellio
ID: 19583803
basically you want to see the last tima computer restarted?

'Calculates system uptime; that is, the number of
'hours a computer has been running since its last restart.



strComputer = "."

strcomputer = inputbox("What is the computername?", "Computer", "computer123")
if strcomputer = "" then strComputer = "."


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 & " Hours"
Next
 
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
0
 
LVL 11

Author Comment

by:bsharath
ID: 19583840
Thankls but can it take all machines from the file and give a report to a file.
0
 
LVL 19

Expert Comment

by:weellio
ID: 19585578
does now


'********
strreadfile = "c:\computers.txt" 'the file with the list of computernames. one per line
strwritefile = "C:\results.txt" ' the results file  = computername : uptime hours

Set objGetComputerList = CreateObject("Scripting.FileSystemObject")
Set fsoReadComputerList = objGetComputerList.OpenTextFile(strreadfile, 1, TristateFalse)
aryComputers = Split(fsoReadComputerList.ReadAll, vbCrLf)
fsoReadComputerList.Close

Set objoutputFile = fso.CreateTextFile(strwritefile, True)
   
For Each strComputer In aryTasks
      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)
                  objoutputFile.WriteLine(strcomputer & ": " & dtmSystemUptime & " Hours")
            Next
next
objoutputFile.close
 
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
'*******
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:bsharath
ID: 19586086
I get this.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Find restarted dates.vbs
Line:      10
Char:      1
Error:      Object required: 'fso'
Code:      800A01A8
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 19

Expert Comment

by:weellio
ID: 19586319
sorry, guess i should test my scripts before i paste them
this works


strreadfile = "c:\computers.txt" 'the file with the list of computernames. one per line
strwritefile = "C:\results.txt" ' the results file  = computername : uptime hours

Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoReadComputerList = fso.OpenTextFile(strreadfile, 1, TristateFalse)
aryComputers = Split(fsoReadComputerList.ReadAll, vbCrLf)
fsoReadComputerList.Close

Set objoutputFile = fso.CreateTextFile(strwritefile, True)
   
For Each strComputer In aryComputers
      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)
                  objoutputFile.WriteLine(strcomputer & ": " & dtmSystemUptime & " Hours")
            Next
next
objoutputFile.close
 
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
0
 
LVL 11

Author Comment

by:bsharath
ID: 19586328
I get this.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Find restarted dates.vbs
Line:      12
Char:      7
Error:      The remote server machine does not exist or is unavailable: 'GetObject'
Code:      800A01CE
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------

Can you get the results as (Machine switched off if it's off)
0
 
LVL 11

Author Comment

by:bsharath
ID: 19586331
I get this.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Find restarted dates.vbs
Line:      12
Char:      7
Error:      0x80041021
Code:      80041021
Source:       (null)

---------------------------
OK  
---------------------------
0
 
LVL 19

Expert Comment

by:weellio
ID: 19586608
there is your answer
The remote server machine does not exist or is unavailable: 'GetObject'

i added error logging.  

strreadfile = "c:\computers.txt" 'the file with the list of computernames. one per line
strwritefile = "C:\results.txt" ' the results file  = computername : uptime hours

Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoReadComputerList = fso.OpenTextFile(strreadfile, 1, TristateFalse)
aryComputers = Split(fsoReadComputerList.ReadAll, vbCrLf)
fsoReadComputerList.Close

Set objoutputFile = fso.CreateTextFile(strwritefile, True)
   
For Each strComputer In aryComputers
      on error resume next
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            If Err.number <> 0 Then
            errlog("Problem with my math")
            end if
        Set colOperatingSystems = objWMIService.ExecQuery _
            ("Select * from Win32_OperatingSystem")
 
            For Each objOS in colOperatingSystems
                  dtmBootup = objOS.LastBootUpTime
                  dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
                  dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
                  objoutputFile.WriteLine(strcomputer & ": " & dtmSystemUptime & " Hours")
                        Whatsup("Writing: "& strcomputer & ": " & dtmSystemUptime & " Hours")
            Next
next
objoutputFile.close
 
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

sub errlog(errmsg)
'turn errors red
      Whatsup("<font color=#FF0000>" & errmsg  & "</font>" )
      Whatsup("<font color=#FF0000>" & Err.Description & " : " & Err.Number & "</font>" )
'Clear error
      Err.Clear  
' Reset error handling
      On Error Goto 0
end sub


Dim oIE
Sub Whatsup(sMsg)
      If Not IsObject(oIE) Then
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate "about:blank"
            oIE.Top = 10
            oIE.left = 10
            oIE.Width = 500
            oIE.Height = 150
            oIE.Visible = True
            oIE.ToolBar = False
            oIE.menubar = False
            oIE.StatusBar = False
            oIE.AddressBar = False
            oIE.Document.Body.Title = "Debug Messages"
      End If
      oIE.Document.writeln now & " - " & smsg & "<br>"
End Sub
0
 
LVL 19

Accepted Solution

by:
weellio earned 2000 total points
ID: 19586612
FYI the error loggin writes to internet explorer
mistyped something.. use this one





strreadfile = "c:\computers.txt" 'the file with the list of computernames. one per line
strwritefile = "C:\results.txt" ' the results file  = computername : uptime hours

Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoReadComputerList = fso.OpenTextFile(strreadfile, 1, TristateFalse)
aryComputers = Split(fsoReadComputerList.ReadAll, vbCrLf)
fsoReadComputerList.Close

Set objoutputFile = fso.CreateTextFile(strwritefile, True)
   
For Each strComputer In aryComputers
      on error resume next
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            If Err.number <> 0 Then
            errlog("Computer won't connect")
            end if
        Set colOperatingSystems = objWMIService.ExecQuery _
            ("Select * from Win32_OperatingSystem")
 
            For Each objOS in colOperatingSystems
                  dtmBootup = objOS.LastBootUpTime
                  dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
                  dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
                  objoutputFile.WriteLine(strcomputer & ": " & dtmSystemUptime & " Hours")
                        Whatsup("Writing: "& strcomputer & ": " & dtmSystemUptime & " Hours")
            Next
next
objoutputFile.close
 
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

sub errlog(errmsg)
'turn errors red
      Whatsup("<font color=#FF0000>" & errmsg  & "</font>" )
      Whatsup("<font color=#FF0000>" & Err.Description & " : " & Err.Number & "</font>" )
'Clear error
      Err.Clear  
' Reset error handling
      On Error Goto 0
end sub


Dim oIE
Sub Whatsup(sMsg)
      If Not IsObject(oIE) Then
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate "about:blank"
            oIE.Top = 10
            oIE.left = 10
            oIE.Width = 500
            oIE.Height = 150
            oIE.Visible = True
            oIE.ToolBar = False
            oIE.menubar = False
            oIE.StatusBar = False
            oIE.AddressBar = False
            oIE.Document.Body.Title = "Debug Messages"
      End If
      oIE.Document.writeln now & " - " & smsg & "<br>"
End Sub

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is my 3rd article on SCCM in recent weeks, the 1st (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_Server_2008/A_4466-A-beginners-guide-to-installing-SCCM2007-on-Windows-2008-R2-Server.html) dealing with installat…
Know what services you can and cannot, should and should not combine on your server.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question