' NAME: LastUserLogon.vbs ' ' AUTHOR: alan dot kaplan at va dot gov ' DATE : 2/3/2012 ' ' COMMENT: Gets the current/last user of a computer using WMI ' '========================================================================== Option Explicit 'ref http://www.devguru.com/Technologies/ado/quickref/record_fieldscollection.html Const adVarChar = 200 Const MaxCharacters = 255 Const adFldIsNullable = 32 Const adInteger = 3 Const adDBTimeStamp = 135 'YYYYMMDDHHMMSS date/time format Dim WshShell: Set WshShell = WScript.CreateObject("WScript.Shell") Dim strComputer Dim objWMIService dim dNewest, strNewest dNewest = #1/1/1980# strNewest = "" Dim ColComputer, obJComputer, strCurrentUser Dim message dim colLogons, oLogon Dim oRS If WScript.Arguments.Count = 1 Then strComputer = WScript.Arguments(0) Else strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") strComputer = InputBox("Check for last user's logon onto what PC","Computer name",strComputer) End If If strcomputer = "" Then WScript.Quit strComputer = UCase(strComputer) On Error Resume Next Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") If Err <> 0 Then MsgBox "Failed to connect to " & strComputer & ". " & Err.Description,vbCritical + vbOKOnly,"Fatal Error" WScript.Quit End If Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") For Each objComputer In colComputer strCurrentUser = objComputer.UserName Next If Len (strCurrentUser) > 1 Then message = "This user is still logged on." Else message = "There are no current logons." End If 'create disconnected recordset -- note the ADOR Set oRS = CreateObject("ADOR.Recordset") oRS.Fields.Append "FullName", adVarChar, MaxCharacters, adFldIsNullable oRS.Fields.Append "LogonName", adVarChar, MaxCharacters, adFldIsNullable oRS.Fields.Append "LastLogon", adVarChar, adFldIsNullable oRS.Fields.Append "NumberofLogons", adInteger, adFldIsNullable oRS.Open Set colLogons = objWMIService.ExecQuery("SELECT Fullname,LastLogon,Name,NumberOfLogons FROM Win32_NetworkLoginProfile where numberofLogons > 0",,48) If Err <> 0 Then MsgBox "Failed to connect to " & strComputer & ". " & Err.Description,vbCritical + vbOKOnly,"Fatal Error" WScript.Quit End If For Each oLogon In colLogons oRS.AddNew oRS("FullName") = oLogon.FullName oRS("LogonName") = oLogon.Name oRS("LastLogon") = WMI2LocalTime(ologon.Lastlogon) oRS("NumberofLogons") = oLogon.NumberOfLogons oRS.Update Next On Error Resume Next oRS.MoveFirst Do Until oRS.EOF GetNewestDate FixNull(oRS.Fields.Item("FullName")), FixNull(oRS.Fields.Item("LogonName")), FixNull(oRS.Fields.Item("LastLogon")) oRS.MoveNext Loop oRS.Close message = "The most recent user to log onto " & strComputer & " is " & _ strNewest & " reported as " & dNewest & ". " & message Dim retval Retval = MsgBox(message & VbCrLf & VbCrLf & "Copy results to clipboard?",vbQuestion + vbYesNo,strComputer) If retval = vbYes Then Copy2Clip(message) ' ========= Functions and Subs ============ Function FixNull(strText) FixNull = strText If IsNull(strText) Then FixNull = "" If IsEmpty(strText) Then FixNull = "" End Function Sub GetNewestDate(strFullName, strName, dLastLogon) If cdate(dLastLogon) > dNewest Then If Len(strFullName) = 0 Then strNewest = strFullName & Space(1) & strName Else strNewest = strFullName & " (" & strName & ")" End If dNewest= dLastLogon End If End Sub Function WMI2LocalTime(strWMITime) Const CONVERT_TO_LOCAL_TIME = True Dim dateTime ' Create a new datetime object. Set dateTime = CreateObject("WbemScripting.SWbemDateTime") 'Set Value dateTime.Value = strWMITime 'Return localtime VT_DATE format. WMI2LocalTime = dateTime.GetVarDate(CONVERT_TO_LOCAL_TIME) Set dateTime = Nothing End Function Function Copy2Clip(strMessage) Dim objWord 'Sets what you want to put in the clipboard using Word object 'Variation of code by Tony Bothwell, posted at 'http://stackoverflow.com/questions/128463/use-clipboard-from-vbscript ' Declare an object for the word application On Error Resume Next Set objWord = CreateObject("Word.Application") If Err = 0 Then ' Using the object With objWord .Visible = False ' Don't show Word .Documents.Add ' Create a document .Selection.TypeText strMessage ' Put text into it .Selection.WholeStory ' Select everything in the doc .Selection.Copy ' Copy contents to clipboard .Quit False ' Close Word, don't save End With WshShell.Popup "Data copied to clipboard",15,"Done" Else Err.Clear If(MsgBox ("Could not copy to clipboard, send to notepad instead?",_ vbYesNo + vbQuestion, "Send to Notepad") = vbYes) then wshshell.Run("notepad.exe") While wshshell.appactivate("Untitled - Notepad") = False Wscript.Sleep 10 Wend wshshell.SendKeys strMessage End If End If On Error GoTo 0 End Function