|
akaplan -> RE: Retrieve Computername using log-in name (6/26/2008 1:03:57 PM)
|
There are two timestamp properties in AD. LastLogon is not replicated. lastLogonTimestamp is a replicated value that can be off by 14 days. So to see who logged in today using AD required you to query every DC for users logged on today. Making matters worse is the fact that you have to do some nasty integer8 stuff to convert the dates into something you can used. However, the computer that they logged on from is not in AD. The data could be in your DC security logs. Another pain to search. If you map home directories, the easiest way to see who is logged in is to look at the session objects to the home directory server. An example of using session object: '************************************************************** 'File: xFinger.vbs 'Author: Gurgen Alaverdian (www.gurgensvbstuff.com) 'Date: 05.2000 ' 'Script should retrieve a list of workstations 'specified user is logged on to. 'Very useful for System Administrators or Help Desk personal. 'For the script to work properly user "Home Drive" UNC path 'must be specified in the user account property. 'When user logged on to the domain, chances are good, that 'at list one resource is accessed on the home server. 'By enumerating sessions on the home server, 'code retrieves workstation name the user is connected from. 'Running script on NT workstation require ADSI v2.5 installed. 'Syntax: xFinger.vbs [domain\user_name] '*************************************************************** '6-2-03 modified by Alan Kaplan for prompted input. Option Explicit Dim inputStr, UserName, Domain, splitInput, sName, oArgs, Session Dim oUser, userFullName, userHome, fService, LoggedOn, q, wshshell Set WshShell = WScript.CreateObject("WScript.Shell") On Error Resume Next q = Chr(34) Set oArgs = Wscript.Arguments 'Check for Script Argument. If not supplied, then 'display proper syntax calling "Syntax" help sub. If oArgs.Count <> 1 Then getinput Else inputStr = oArgs(0) If InStr(inputStr, "\") <> 0 Then splitarg inputstr else GetInput End If End If 'Connect to SAM database. Error will be raised if 'domain or user name is invalid. Set oUser = GetObject("WinNT://" & Domain & "/" & UserName & ",user") If Err.Number <> 0 Then MsgBox "Cannot locate Domain " & _ q & Domain & q & " or user account " & _ q & UserName & q & " does not exists.", _ 64, "Error:": Wscript.Quit End If 'Get user's Full Name and Home Drive account property. 'User home drive property is used to retrieve "Home" server name. 'If this property is not defined then display error and exit. userFullName = oUser.FullName userHome = oUser.HomeDirectory If userHome = "" Then MsgBox "Home Server for user " & _ q & UserName & q & " not specified." , _ 64, "Error:": Wscript.Quit End If 'Retrieve server name. sName = Split(userHome, "\") 'Connect to home server and create a session object. Set fService = GetObject("WinNT://" & sName(2) & "/LanmanServer") If Err.Number <> 0 Then MsgBox "Can not connect to user Home Server. Server is down, or you do not have enough privileges!",64, "Error:": Wscript.Quit End If 'Enumerate current sessions on the server matching session users 'to the given user name. 'If match is found retrieve computer name 'the user is connected from and build a '"LoggedOn" list string. For Each Session In fService.Sessions If LCase(Session.User) = LCase(UserName) Then LoggedOn = LoggedOn & vbCr & Session.Computer End If Next 'If string is not empty then display it in the message box. If LoggedOn = "" then MsgBox "User " & q & UserName & q & " (" & _ userFullName & ") is not logged on.", 64, "Query:" Else: MsgBox "User " & q & UserName & q & " (" & _ userFullName & ") found logged on to the following systems: " & _ vbcr & vbcr & LoggedOn, , "Query" End If Sub Syntax() MsgBox "SYNTAX: xFinger.vbs [Domain\User_Name]" & vbcr & vbcr & _ "Domain - Focus domain." & vbcr & _ "User_Name - User name to query for." & vbcr & vbcr & _ "NOTE: For the script to work, user must" & _ " have a home drive specified" & vbcr &_ " in the domain account properties." & vbcr, 64, "Syntax:" End Sub Function splitarg (inputStr) splitInput = Split(inputStr, "\") Domain = splitInput(0) UserName = splitInput(1) End Function Sub getinput Domain = wshshell.ExpandEnvironmentStrings("%userdomain%") Dim myname myname = wshshell.ExpandEnvironmentStrings("%username%") UserName = InputBox("Enter Domain\Username as shown:","Locate user logons",Domain&"\"&myname) 'If domain or user name is empty call a help sub. splitarg username If Domain = "" Or UserName = "" Then Syntax End If End Sub You could use AD to get a list of computers, then return who is logged on now. Example code for current user: 'Alan Kaplan 2003 Option Explicit dim wshShell Dim otrans dim message, strComputer Set wshShell = WScript.CreateObject("WScript.Shell") strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") 'On Error Resume Next If WScript.Arguments.Count = 1 Then strComputer = WScript.Arguments(0) Else strComputer = InputBox("Get Name of logged on user on what computer?","Logged on User",strComputer) End If If strComputer = "" Then WScript.Quit strComputer = UCase(strComputer) msgbox RemoteComputerUsers(strcomputer),vbokonly + vbinformation,"Logged onto " & strComputer WScript.Quit '======= Function ========== Function RemoteComputerUsers (strComputer) Dim objLocator, objWMIService, objUserInfoList, objUserInfo Dim strDom strDom = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%") Set oTrans = CreateObject("NameTranslate") oTrans.Init 3, strDom ' initialize with NT style Domain name If Err <> 0 Then MsgBox "Could not contact an NT domain" ,vbokonly + vbcritical,"Fatal Error" WScript.Quit End If set objLocator = CreateObject("WbemScripting.SWbemLocator") Set objWMIService = objLocator.ConnectServer(strComputer) If Err.Number <> 0 Then MsgBox "Error reaching or connecting to WMI on " & strComputer,vbcritical & vbokonly, "Failed" WScript.Quit End If Set objUserInfoList = objWMIService.InstancesOf("Win32_ComputerSystem") If (Err.Number = 0) Then For Each objUserInfo in objUserInfoList WScript.Echo objUserInfo.UserName If Not isnull(objUserInfo.Username) Then message = message & objUserInfo.UserName & vbTab oTrans.Set 8,objUserInfo.UserName 'name here is in domain\samname format message = message & "(" & oTrans.Get(4) & ")" & vbcrlf 'Get Fullname End If Next Else message = Err.Description End If If message = "" Then message = "No one logged onto " & strComputer End If RemoteComputerUsers = message End Function Wscript.Quit
|
|
|
|