Solved

retrieve the system uptime

Posted on 2007-12-04
5
406 Views
Last Modified: 2008-02-01
This script below provides a bunch of info about the computer after I enter the computer name.
I would like to know if I can see another info which is the system uptime( since when the computer hasn't been restarted.)

Thanks

<head>
<title>Login Details of a Machine</title>
<HTA:APPLICATION
     APPLICATIONNAME="Login Details"
     BORDER="thin"
     SCROLL="no"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
>
</head>

<script language="VBScript">

Sub Window_onLoad
      Me.ResizeTo 400,300
    Me.MoveTo ((Screen.Width / 2) - 200),((Screen.Height / 2) - 150)
End Sub

Sub Default_Buttons
      If Window.Event.KeyCode = 13 Then
            btn_logindetails.Click
      End If
End Sub

Sub Get_Login_Details
   
      strComputer = txt_computer.value
     
      if strComputer = "" Then
            msgbox "Please enter a computer name or IP Address",vbOKOnly,"No Computer Name or IP Address"
            Exit Sub
      End If
     
      If Ping(strComputer) = False Then
        msgbox strComputer & " did not respond to ping. No action could be taken.",vbOkOnly, "Cannot continue script."
        Exit Sub
      End If      
     
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     
      Set colComputer = objWMIService.ExecQuery _
          ("Select * from Win32_ComputerSystem")
     
      Set colComputerIP = objWMIService.ExecQuery _
          ("Select * from Win32_NetworkAdapterConfiguration")
     
      Set colSystemInfo = objWMIService.ExecQuery _
          ("Select * from Win32_OperatingSystem",,48)
       
      For Each objComputer in colComputer
          strUserName = "User Name: " & objComputer.UserName
          strHostName = "Host Name: " & objComputer.Name
      Next
     
      For Each IPConfig in colComputerIP
            If Not IsNull(IPConfig.IPAddress) Then
              For intIPCount = LBound(IPConfig.IPAddress) _
                  to UBound(IPConfig.IPAddress)
                      strIPAddress = strIPAddress & "IP Address: " & IPConfig.IPAddress(intIPCount) & "~"
            next
            end if
      Next
     
      If Right(strIPAddress, 1) = "~" Then
            strIPAddress = Left(strIPAddress, Len(strIPAddress) - 1)
      End If
      strIPAddress = Replace(strIPAddress, "~", vbCrLf)
     
      For Each objItem in colSystemInfo
           strOS_Caption = "Caption: " & objItem.Caption
           strOS_SPVersion = "SP Version: " & objItem.CSDVersion
           strOS_VerNumber = "Version Number: " & objItem.Version
      Next
     
      MsgBox strUserName & vbcrlf & strHostName & vbcrlf & _
           strIPAddress & vbcrlf & strOS_Caption & vbcrlf & _
           strOS_SPVersion & vbcrlf & strOS_VerNumber, vbOKOnly, "Login Details"
   
End Sub

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

</script>

<body STYLE="font:14 pt arial; color:white;filter:progid:DXImageTransform.Microsoft.Gradient
(GradientType=1, StartColorStr='#000033', EndColorStr='#0000FF')" onkeypress='vbs:Default_Buttons'>
      <table width='80%' height = '100%' align='center' border='0'>
            <tr height='20%'>
                  <td>
                  </td>
            </tr>
            <tr height='10%'>
                  <td align='center'>
                        Computer name or IP Address:
                  </td>
            </tr
            <tr height='20%'>
                  <td align='center'>
                        <input type="text" value="172.16.2.48" name="txt_computer" maxlength='15' size='16'>
                  </td>
            </tr>
            <tr height='30%'>
                  <td align='center'>
                        <input type="button" value="Check Machine" name="btn_logindetails"  onClick="Get_Login_Details">
                  </td>
            </tr>
            <tr height='20%'>
                  <td>
                  </td>
            </tr>

      </table>

</body>
'===========
0
Comment
Question by:jskfan
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 20406712
For boot time, lopok in the system event log for type 6009 entries.
0
 
LVL 12

Accepted Solution

by:
chandru_sol earned 500 total points
ID: 20408369
Try this script jskfan..........

Put the computers list in the c:\computers.txt


regards
Chandru
'Constant declarations for File I/O

Const ForReading = 1

Const ForWriting = 2

Const ForAppending = 8
 

set fs = CreateObject("Scripting.FileSystemObject")
 

strPath = "C:\computers.txt" 'Path to text file listing servers
 

if fs.FileExists(strPath) then

	set fsInput = fs.OpenTextFile(strPath,ForReading)

		do until fsInput.AtEndOfStream

			strComputer = fsInput.ReadLine

			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 "Computer " & strComputer & "

has been up for " & dtmSystemUptime & " Hours"

			Next

		loop
 

End if
 

wscript.echo "Script Complete"
 

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

Open in new window

0
 

Author Comment

by:jskfan
ID: 20411904
excellent!!!!!!!!!!
0
 

Author Comment

by:jskfan
ID: 20411907
'Constant declarations for File I/O
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
 
set fs = CreateObject("Scripting.FileSystemObject")
 
strPath = "C:\computers.txt" 'Path to text file listing servers
 
if fs.FileExists(strPath) then
      set fsInput = fs.OpenTextFile(strPath,ForReading)
            do until fsInput.AtEndOfStream
                  strComputer = fsInput.ReadLine
                  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 "Computer " & strComputer & "has been up for " & dtmSystemUptime & " Hours"
                  Next
            loop
 
End if
 
wscript.echo "Script Complete"
 
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
 
'Open in New Window Accept Multiple Solutions Accept as Solution
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20411962
Great!

regards
Chandru
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now