Login | |
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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.
|
|
| |
|
|
|
 |
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!
|
|
| |
|
|
|
|
|