By PinkelCreated 31 Mar 2011, Modified 01 Apr 2011
My Rating:
Vote
Rating:
(1 votes)
Views:34737
Downloads:569
Source:

Query Active Directory for User(s) last login

Language:  VBScript

Compatibility

Windows XP Yes Windows 2003 Yes
Windows 2000 Yes Windows NT Unknown
Vista Yes Windows 2008 Yes
Description

Queries the Active Directory/Domain for a user last login time or all users (output in .csv file) last loging older then X days. The necessary info will be requested.

Code

Line Numbers: On  Off      Plain Text
'******************************************************************
' naam:     ActiveADUsers.vbs
' function: Query in Active Directory for user(s) Last Login
' options:  The necessary info will be requested
' author:   Pinkel
' versie:   1.2
' datum:    31-03-2011
'******************************************************************
On Error Resume Next
Dim objFileSys, objLogFile
Dim objDomain
strDomain=InputBox("Enter the FQN domain in:","Check Active AD Users","DomainName.local")
If strDomain="" Then
	wscript.echo "Script canceled, no FQN domain."
	wscript.quit
Else
	If strDomain="DomainName.local" Then
		wscript.echo "Script canceled, no FQN domain."
		wscript.quit
	End If
End If
strADomain = Split(strDomain, ",")
Set objDomain = GetObject("WinNT://" & strADomain (0))
If err.number<>0 Then
	wscript.echo "Error, no contact with " & objDomain
	wscript.quit
End If
strUserName=InputBox("Enter the user name:" & vbCrLf & _
	"(when blanck, all users)" & vbCrLf & _
	"Please note, case sensitive","Check Active AD Users","")
If strUserName="" Then
	strNumDays=InputBox("The number of days not logged in?","Check Active AD Users","180")
	If strNumDays="" Then
		wscript.echo "Script canceled, no number of days."
		wscript.quit
	Else
		objOlderThen = Now - strNumDays
	End If
	strLogName=InputBox("Enter the name of csv file in:","Check Active AD Users","DomainUsers.csv")
	If strLogName="" Then
		wscript.echo "No csv file entered!"
		wscript.quit
	End If
	Set objFileSys = CreateObject("Scripting.FileSystemObject") 
	Set objLogFile = objFileSys.CreateTextFile(strLogName, True) 
	objLogFile.write "UserName;Full Name;Lastlogin;Description" & vbNewLine
	objDomain.Filter = Array("user")
	For Each objUser In objDomain
	    strDisplayName = objUser.FullName
		strUserLogin = objUser.LastLogin
		strDescription = objUser.Description
		If UBound(strADomain) >= 1 Then
			For ii = 1 To UBound(strADomain)
				Set strAccount = GetObject("WinNT://" & strADomain(ii) & "/" & objUser.Name & ",user")
				If strAccount.LastLogin > strUserLogin Then strUserLogin = strAccount.LastLogin
			Next
		End If
		If strUserLogin < objOlderThen Then
			objLogFile.write Left(objUser.Name,22) & ";" & strDisplayName & ";" & FormatDateTime(strUserLogin) & ";" & strDescription & vbNewLine
		End If
	Next
	objLogFile.close
	Wscript.echo "Script ActiveADUsers.vbs ready."
Else
	objDomain.Filter = Array("user")
	
	For Each objUser In objDomain
		strUserLogin = objUser.LastLogin
		If UBound(strADomain) >= 1 Then
			For ii = 1 To UBound(strADomain)
				Set strAccount = GetObject("WinNT://" & strADomain(ii) & "/" & objUser.Name & ",user")
				If strAccount.LastLogin > strUserLogin Then strUserLogin = strAccount.LastLogin
			Next
		End If
		If objUser.Name = strUserName Then
			WScript.Echo "User " & Left(objUser.Name,22) & " last login is: " & FormatDateTime(strUserLogin)
			Set objDomain=Nothing
			Set Shell=Nothing
			WScript.Quit
		End If
	Next
	Wscript.echo "Uer " & strUserName & " not found. Please note, case sensitive."
End If
Set objDomain=Nothing
Set Shell=Nothing
Wscript.Quit

 


Got a useful script? Click here to upload!


 

  Post Comment
Order By:  
User Comments
      
Be the first to post a comment!