Welcome !
         

 Get current user and uptime

Author Message
netmarcos

  • Total Posts : 55
  • Scores: 0
  • Reward points : 0
  • Joined: 12/7/2004
  • Location: USA
  • Status: offline
Get current user and uptime Wednesday, March 15, 2006 5:44 AM (permalink)
0
'********************************************************************************
'* LogonUptime.vbs - Collect and display currently logged on user and uptime
'*      for a remote computer.
'*
'* 03/15/2006 - Mark M. Webster - 1.0
'********************************************************************************
Option Explicit
Dim strComputerName
Dim objWMIServices
Dim objUserSet
Dim oWshShell
Dim User
Dim colOperatingSystems
Dim objOperatingSystem
On Error Resume Next
Set oWshShell = CreateObject("Wscript.Shell")
'************************************************************************************************
'* Get user input for computer name. If nothing is entered, this will default to the local system
'************************************************************************************************
strComputerName = InputBox ("Enter the name of the computer you wish to query","Target system", ".")
'*  Configure WMI connection properties for target system
objWMIServices = "winmgmts:{impersonationLevel=impersonate}!//"& strComputerName &""
'****************************************************************************************************
'* Connect to WMI and get Computer system properties
'* If connection is not successful, either prompt for alternate credentials or display error and exit
'****************************************************************************************************
Set objUserSet = GetObject( objWMIServices ).InstancesOf ("Win32_ComputerSystem")
If Err.number <> 0 Then
If Err.Number = "-2147217405" Then
 btnCode = oWshShell.Popup ("Access Denied" & Chr(13) & "Try again with alternate credentials?" , 30, strComputerName, 4+32)
 
 Select Case BtnCode
    case 6       altCreds()
    case 7       oWshShell.Popup "Process has been cancelled by user",5,"Notice...",16
     wScript.Quit
    case -1     oWshShell.Popup "No user input. Process has been aborted.",10,"Notice...",64
     wScript.Quit  
 End Select
 
ElseIf Err.Number = "462" Then
 oWshShell.Popup "Host Unreachable", 10, strComputerName, 48
Else
 oWshShell.Popup "Attempt to query current user on: " & strComputerName & " has failed." & Chr(13) & Err.Number & " : " & Err.Description, 10, strComputerName, 48
End If
Err.Clear
 wScript.Quit
Else
Set colOperatingSystems = GetObject( objWMIServices ).InstancesOf ("Win32_OperatingSystem")

If Err.Number <> 0 Then WScript.Echo Err.Number & " : " & Err.Description
Err. Clear
End If
'********************************
'* Look for user name in data set
'********************************
for each User in objUserSet
If User.UserName <> "" Then
    oWshShell.Popup "The current user on " & strComputerName & " is: " & User.UserName & Chr(13) & upTime(strComputerName), 10, strComputerName, 64
Else
    oWshShell.Popup "There are no users currently logged in at " & strComputerName & Chr(13) & upTime(strComputerName), 10, strComputerName, 64
End If
Next
'* End of script processing
'**************************

'********************************************************************************
'* Function upTime() retrieves last boot time from system and calculates uptime
'*     
'* Sets function value to string declaring uptime in Hours, Minutes & seconds
'********************************************************************************
Function upTime(strComputer)
Dim objOS
Dim dtmBootup
Dim dtmLastBootupTime
Dim dtmSystemUptime
On error Resume Next
upTime = 0
For Each objOS in colOperatingSystems
   dtmBootup = objOS.LastBootUpTime
   dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
   dtmSystemUptime = "Last system reboot occurred " & DateDiff("h", dtmLastBootUpTime, Now) & " hours, " & Int(DateDiff("n", dtmLastBootUpTime, Now)/60) & " minutes, " & DateDiff("n", dtmLastBootUpTime, Now) Mod 60 & " seconds ago." 
If Err.Number =0 Then
upTime = dtmSystemUptime
Else
upTime = "Last reboot time cannot be retrieved from " & strComputer
End If
Err.Clear
Next
End Function
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

'********************************************************************************
'* Function altCreds() Prompts user ot enter name and password for use in
'*        establishing WMI connection if current credentials fail
'********************************************************************************
Function altCreds(sHost)
Dim sUser
Dim sPass
Dim oSWbemLocator
Dim oSWbemServices

sUser = InputBox("Please enter the Administrator Name: ")
sPass = InputBox("Please enter the administrator password: ")
On Error Resume Next
Set oSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set oSWbemServices = oSWbemLocator.ConnectServer _
   (sHost, "root\cimv2", sUser, sPass)

If Err.Number <> 0 Then
oWshShell.Popup "WMI Connection was not successful. "  & Chr(13) & Err.Description, 10, Err.Source & " on " & sHost, 48
Else
Set objUserSet = oSWbemServices.ExecQuery ("Select * from Win32_ComputerSystem")
Set colOperatingSystems = oSWbemServices.ExecQuery ("Select * from Win32_OperatingSystem")

End If
End Function


Mark M Webster
 
#1
    Talapakula

    • Total Posts : 3
    • Scores: 0
    • Reward points : 0
    • Joined: 5/31/2006
    • Status: offline
    RE: Get current user and uptime Wednesday, May 31, 2006 3:34 PM (permalink)
    0
    Dim objNet
    On Error Resume Next
     
    Set objNet = CreateObject("WScript.NetWork")
    If  Err.Number <> 0 Then              
     
    MsgBox "Its OK" & vbCRLF &_
                   "Do not press ""No"" If your browser warns you."
     Document.Location = "UserInfo.html"  
                                            'Place the Name of the document.
                                     'It will display again
    End if
     
    Dim strInfo
    strInfo = "User Name is     " & objNet.UserName & vbCRLF & _
              "Computer Name is " & objNet.ComputerName & vbCRLF & _
              "Domain Name is   " & objNet.UserDomain
    MsgBox strInfo
     
    Set objNet = Nothing
    Raj
     
    #2
      ginolard

      • Total Posts : 1347
      • Scores: 21
      • Reward points : 0
      • Joined: 8/11/2005
      • Status: offline
      RE: Get current user and uptime Tuesday, June 13, 2006 9:59 PM (permalink)
      0
      That's a nice script but as with most scripts that attempt to get the logged on user it won't return a result if

      a) a user is logged on remotely
      b) a user is logged on locally.

      The only reliable way I've found to circumvent this is to query the owner of the explorer.exe process.
      Author of ManagePC - http://managepc.net

       
      #3
        Meg

        • Total Posts : 123
        • Scores: 6
        • Reward points : 0
        • Joined: 7/13/2006
        • Location: Australia
        • Status: offline
        RE: Get current user and uptime Saturday, September 02, 2006 11:08 PM (permalink)
        0
        I have just had a go at doing just that ginolard
        http://www.visualbasicscript.com/m_37477/mpage_1/key_/tm.htm#37477

         
        #4
          ginolard

          • Total Posts : 1347
          • Scores: 21
          • Reward points : 0
          • Joined: 8/11/2005
          • Status: offline
          RE: Get current user and uptime Tuesday, September 12, 2006 3:01 AM (permalink)
          0
          There is one slight drawback to this method and it only occurs on certain Win2k configurations (and I don't know why).  On certain Win2K machines, you get an Access Denied error when attempting to get the owner of the explorer.exe process.
          Author of ManagePC - http://managepc.net

           
          #5
            Meg

            • Total Posts : 123
            • Scores: 6
            • Reward points : 0
            • Joined: 7/13/2006
            • Location: Australia
            • Status: offline
            RE: Get current user and uptime Thursday, September 21, 2006 3:16 PM (permalink)
            0
            Thanks for that,
            That is a good point to note. I didn't come across that error with the W2k machines on my network.
            Our machines are all locked into a SOE so that limits different configurations.
            Also we are in the process of converting our SOE to XP
             
            #6
              ginolard

              • Total Posts : 1347
              • Scores: 21
              • Reward points : 0
              • Joined: 8/11/2005
              • Status: offline
              RE: Get current user and uptime Thursday, December 07, 2006 8:41 PM (permalink)
              0
              I just noticed another problem with the script as I wanted to use it.

              It does not take into account that you might be using an alternative date/time format

              For example, on my machine dtmLastBootupTime = 12/08/2006 08:46:23 and Now() = 08/12/2006 09:39:25

              So the result is something like 2833 hours, 2822 mins whereas it should be 0 hours, 47 mins.

              I just needed the change the WMIDateStringToDate function so that it rebuilt the date in the correct format but you might want to add some sort of check in there to see which date format the user is using.


              <message edited by ginolard on Thursday, December 07, 2006 8:55 PM>
              Author of ManagePC - http://managepc.net

               
              #7
                Country73

                • Total Posts : 754
                • Scores: 14
                • Reward points : 0
                • Status: offline
                RE: Get current user and uptime Friday, December 08, 2006 3:06 AM (permalink)
                0
                Actually, can't you just use the PSLOGGEDON -l \\ComputerName for this? (www.sysinternal.com)

                This will list just the local logons, and what Date/Time they logged on.
                 
                #8
                  ginolard

                  • Total Posts : 1347
                  • Scores: 21
                  • Reward points : 0
                  • Joined: 8/11/2005
                  • Status: offline
                  RE: Get current user and uptime Monday, December 18, 2006 2:41 AM (permalink)
                  0
                  Well, yes, you could use PsLoggedOn but that's cheating ;)
                  Author of ManagePC - http://managepc.net

                   
                  #9
                    Country73

                    • Total Posts : 754
                    • Scores: 14
                    • Reward points : 0
                    • Status: offline
                    RE: Get current user and uptime Monday, December 18, 2006 4:13 AM (permalink)
                    0
                    OK, so I'm a cheater, but atleast I don't have to type as much code to get the output I'm looking for!

                     
                    #10
                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2017 ASPPlayground.NET Forum Version 3.9