Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Get current user and uptime

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,32298
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Get current user and uptime
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Get current user and uptime - 3/15/2006 4:44:26 AM   
  netmarcos

 

Posts: 55
Score: 0
Joined: 12/7/2004
From: USA
Status: offline
'********************************************************************************
'* 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
 
 
Revisions: 1 | Post #: 1
 
 RE: Get current user and uptime - 5/31/2006 2:34:07 PM   
  Talapakula

 

Posts: 2
Score: 0
Joined: 5/31/2006
Status: offline
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

(in reply to netmarcos)
 
 
Post #: 2
 
 RE: Get current user and uptime - 6/13/2006 8:59:36 PM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
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
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to Talapakula)
 
 
Post #: 3
 
 RE: Get current user and uptime - 9/2/2006 10:08:56 PM   
  Meg


Posts: 125
Score: 2
Joined: 7/13/2006
From: Australia
Status: offline
I have just had a go at doing just that ginolard
http://www.visualbasicscript.com/m_37477/mpage_1/key_/tm.htm#37477

(in reply to ginolard)
 
 
Post #: 4
 
 RE: Get current user and uptime - 9/12/2006 2:01:12 AM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
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
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to Meg)
 
 
Post #: 5
 
 RE: Get current user and uptime - 9/21/2006 2:16:38 PM   
  Meg


Posts: 125
Score: 2
Joined: 7/13/2006
From: Australia
Status: offline
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

(in reply to ginolard)
 
 
Post #: 6
 
 RE: Get current user and uptime - 12/7/2006 7:41:59 PM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
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 -- 12/7/2006 7:55:16 PM >


_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to Meg)
 
 
Revisions: 1 | Post #: 7
 
 RE: Get current user and uptime - 12/8/2006 2:06:59 AM   
  Country73


Posts: 712
Score: 8
Joined: 8/25/2004
From: USA
Status: offline
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.

(in reply to ginolard)
 
 
Post #: 8
 
 RE: Get current user and uptime - 12/18/2006 1:41:49 AM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
Well, yes, you could use PsLoggedOn but that's cheating ;)

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to Country73)
 
 
Post #: 9
 
 RE: Get current user and uptime - 12/18/2006 3:13:25 AM   
  Country73


Posts: 712
Score: 8
Joined: 8/25/2004
From: USA
Status: offline
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!

(in reply to ginolard)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Get current user and uptime Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts