Announcement

Collapse
No announcement yet.

Script to determine windows uptime

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to determine windows uptime

    Im not a scripting guru or anything. But i found this MS article on determine windows uptime via script

    http://www.microsoft.com/technet/scr...5/hey0802.mspx

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

    For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("n", dtmLastBootUpTime, Now)
    Wscript.Echo dtmSystemUptime & " minutes"
    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



    What I want to do is have this script read from a txt file of servers. Say "serverlist.txt" then i want it to drop its results into another txt file, say "output.txt" that has the uptime info..

    Could this be done? Any help is greatly appreciated.

  • #2
    Re: Script to determine windows uptime

    This should (not tested) read a text file and output to the screen each server:

    Code:
    Const ForReading = 1
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile("c:\servers.txt", ForReading)
    
    Do While objTextFile.AtEndOfStream <> True
      strComputer = objtextFile.ReadLine
    
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * From Win32_PerfFormattedData_PerfOS_System")
    
    For Each objOS in colOperatingSystems
    	intSystemUptime = Int(objOS.SystemUpTime / 60)
    	Wscript.Echo strComputer & ": " & intSystemUptime & " minutes"
    Next
    
    set objWMIService = nothing
    set colOperatingSystems = nothing
    
    Loop
    Ill look at outputting to a text file if this works.
    Last edited by tonyyeb; 28th February 2007, 14:44.
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Script to determine windows uptime

      Script updated. Now works.

      (Thread tidyed up)
      Server 2000 MCP
      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Script to determine windows uptime

        yep, this works.. but it gives me the results in a pop up box that shows

        "myserver 2000095 minutes"

        i know im asking alot here.. but with the command "net stats srv" at the command line I get the following info showing when the last reboot happened.

        Statistics since 2/15/2007 4:17 AM

        Could i get it in something other than 2000095 minutes, maybe days, hours.. and get that populated out to a txt file since i plan on running this against 100 servers at a time

        Comment


        • #5
          Re: Script to determine windows uptime

          Well yes it is very easy to do but dont you think it would be helpful for yourself to try to figure it out on your own? Would you not like to learn how to do this?
          Server 2000 MCP
          Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          Comment


          • #6
            Re: Script to determine windows uptime

            i hear what your saying.. but i just need a script to check for uptime for a few servers. im mostly and NT admin not really a scripting guy. maybe someday down the road.

            any help would be appreciated.

            Comment


            • #7
              Re: Script to determine windows uptime

              Ok here is an amendment:

              Code:
              Const ForReading = 1
              
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              Set objTextFile = objFSO.OpenTextFile("c:\servers.txt", ForReading)
              
              Do While objTextFile.AtEndOfStream <> True
                strComputer = objtextFile.ReadLine
              
              Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
              Set colOperatingSystems = objWMIService.ExecQuery("Select * From Win32_PerfFormattedData_PerfOS_System")
              
              For Each objOS in colOperatingSystems
              	intSystemUptime = objOS.SystemUpTime
              	intSystemUptime = 0 - intSystemUptime
              	strUptime = dateadd("s",intSystemUptime,Now())
              	Wscript.Echo strComputer & " has been up since: " & strUptime
              Next
              
              set objWMIService = nothing
              set colOperatingSystems = nothing
              
              Loop
              Server 2000 MCP
              Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

              Comment


              • #8
                Re: Script to determine windows uptime

                To raveman7, try to spend some time to understand more of scripting (atleast during the time others spent hours to answer your question. well this is a Knowledge sharing forum, not a deliver free service forum), you realy can have benefit from that. And there is no such thing as one perfect solution in vbs, any interactive input from your part can be helpfull writing a worthful script.

                Nice script Tony.

                Alternatively to 'tailor made' vbs, there is a command-line tool in Windows, that will show the uptime in day's, hours, minutes:
                From Start / Run:
                Code:
                cmd /k for /f %i in (c:\SERVERnameS.txt) do @echo %i: && SYSTEMINFO.exe /S "%i" /FO CSV /NH >>"c:\results.csv"
                This outputs in csv and conaitins a lof of other columns.

                (And there is also a "WMIC.exe ..call.." command-line that can write the hostnamea and uptime to a file, but that would cost me to much time to find out)


                You could also loop (by using for/next ) SYSTEMINFO.exe as a subroutine in a basic VBscript. It gives you the posibity to add a row of columns heads to the (only the) first line in the outputfile.
                I will publish that basic script in my next post.


                One step after step further;
                In next versions of that script you could add even one of these functions to the VBscript:

                1. Create the possibility to give Administrator credentials from the command line. Or when that is empty, pause the script for two seconds to give the possibility to put in the credentials by inputbox. If no credentials given, then the script automaticaly continuous after two seconds and use the user's credentials to logon to the servers.

                2. Add a date to the output file. And delele earlier output files older than...
                (in case you want to schedule the script. Or if you like to use it as a LOGOF script for computers)

                3. Query OU's in the ActiveDirectory for computeraccounts, instead of using an inputfile. So you can automaticly connect to all the workstations.

                ect...

                bonus (you can ask your self why);
                * write SYSTEMINFO.exe first to a temporary file, then select only the columns you want, and in what order to like to have them. And write that to the final outputfile.

                \RemS

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Script to determine windows uptime

                  Or you could just try this neat little tool from MS

                  http://support.microsoft.com/kb/232243

                  To use it i copied the exe from where i saved it to system32 folder.

                  To actually run i opened a command prompt and typed uptime

                  *EDIT*

                  I forgot to mention this works on NT4, 2000 and 2003
                  Last edited by wullieb1; 1st March 2007, 11:52.

                  Comment


                  • #10
                    Re: Script to determine windows uptime

                    Nice tool Wullieb1

                    uptime.exe
                    I found that there are two different? downloads of that tool on the MS site,
                    I downloaded this one for testing:
                    http://www.microsoft.com/downloads/d...displaylang=en

                    You can use "uptime.exe" (instead of "SYSTEMINFO.exe") in the command-line.
                    Code:
                    for /f %i in (c:\SERVERnameS.txt) do @echo %i: && uptime.exe /S "%i" >>"c:\results.TXT"
                    note:
                    uptime.exe may only run on intel x86 ?

                    \Rem

                    I did NOT tried this one
                    http://www.microsoft.com/downloads/d...displaylang=en
                    Last edited by Rems; 1st March 2007, 12:58.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Script to determine windows uptime

                      I think it'll depend on what the OP wants to achieve as to what the end result needs to be.

                      If they just want to check it then they can follow the instructions i gave.

                      If they want it a bit more advanced then they will need to follow your script.

                      The choice is there.

                      Oh i don't have an x64 system to check it on.

                      Comment


                      • #12
                        Re: Script to determine windows uptime

                        thanks guys. where would be a good place to start for learning more about scripting.

                        Comment


                        • #13
                          Re: Script to determine windows uptime

                          Originally posted by raveman7 View Post
                          thanks guys. where would be a good place to start for learning more about scripting.
                          Best way is to come up with a problem and work towards a solution (searching / adapting / only asking for help when really stuck).

                          I'll take a set of manuals and a real business problem with a tech expert in the wings when I get off base over a week + of schooling and I'll be far ahead of the graduate every time.

                          There are many scripting sites / examples. Understanding the reference material is always a plus:

                          http://msdn2.microsoft.com/en-us/library/ms950396.aspx
                          Cheers,

                          Rick

                          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                          © 2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

                          Comment


                          • #14
                            Re: Script to determine windows uptime

                            Originally posted by Rik
                            Best way is to come up with a problem and work towards a solution
                            I totally agree!!!
                            that is the best way to learn the script language and learning to think in scripting methodes.

                            "a good place to start for learning more about scripting"
                            collections of script examples for download:
                            http://www.microsoft.com/downloads/d...displaylang=en
                            http://www.microsoft.com/downloads/d...DisplayLang=en

                            and check the video's here
                            http://www.microsoft.com/technet/scr...s/archive.mspx
                            ==> "Look, Mom, Iím a Scripting Guy! (Level 200)"
                            ==> "Trading Scripts (Level 200)"
                            (you maid need some spicy potato chips along with it)


                            \Rem

                            This posting is provided "AS IS" with no warranties, and confers no rights.

                            __________________

                            ** Remember to give credit where credit's due **
                            and leave Reputation Points for meaningful posts

                            Comment

                            Working...
                            X