List All Users Last Login, Account And Password Expiration Details

Questo script permette di estrarre da Active Directory alcuni dettagli relativi agli account utente, nello specifico per ogni utente verranno mostrati i seguenti valori (separati da tabulazione):

  • nome utente
  • stato dell'account (abilitato/disabilitato)
  • data di scadenza dell'account
  • scadenza password (abilitata/disabilitata)
  • data di scadenza della password
  • data dell'ultima modifica password
  • data dell'ultimo login eseguito

La ricerca degli oggetti utente non deve necessariamente interessare l’intero dominio dal momento che lo script prevede la possibilità di limitarla ad una determinata unità organizzativa e ad uno specifico numero di sottolivelli (profondità della ricerca) all'interno di essa.

E' necessario specificare correttamente il BaseDN ed il nome del dominio in formato LDAP (per farlo dovete aver chiaro il concetto di percorso LDAP), queste impostazioni sono assolutamente necessarie poiché identificano la base di partenza per la ricerca. Per approfondimenti su LDAP leggete LDAP per Amministratori Active Directory.

Lo script non gestisce in maniera autonoma le operazioni di scrittura dei risultati su file perciò vi consiglio di eseguirlo per mezzo del prompt dei comandi utilizzando il motore WSH “cscript.exe”, in questo modo potrete reindirizzarne facilmente l’output in un file di testo.

cscript.exe list_all_users_lastlogin_account_and_password_expiration_details.vbs > output.txt

ADS_SCOPE_SUBTREE (riga 7), strLDAPDomain (riga 10) e strLDAPBaseDN (riga 13) sono le uniche variabili da modificare nel codice sorgente.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'ADS_SCOPE_SUBTREE specifies the depth of the search operation performed against the BaseDN
'I suggest you to use cscript.exe from a command prompt to redirect the output of this script on a text file
 
On Error Resume Next
 
'Set here the depth of the search operation performed against the BaseDN
Const ADS_SCOPE_SUBTREE = 30
 
'Set here the LDAP Domain Name
strLDAPDomain = "dc=domain,dc=lan"
 
'Set here the LDAP BaseDN
strLDAPBaseDN = "dc=domain,dc=lan"
 
Const ADS_UF_ACCOUNTDISABLE  = 2
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
objCommand.CommandText = _	
	"SELECT ADsPath,Name,userAccountControl FROM 'LDAP://" & strLDAPBaseDN &"' WHERE objectCategory='User'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Wscript.Echo "User Name" & vbTab & "Account Status" & vbTab & "Account Expiration Date" & vbTab & "Password Status" & vbTab & "Password Expiration Date: " & vbTab & "Last Password Change: " & vbTab & "Last Login: "
 
Do Until objRecordSet.EOF
	strPath = objRecordSet.Fields("ADsPath").Value
	Set objUser = GetObject(strPath)
	Set objDomain = GetObject("LDAP://" & strLDAPDomain)
	strPasswordChangeDate = ""
	strAccExpDate = ""
	strLastLogin = ""
	strAccountDisabled =""
	'strAccountDisabled ="Account Enabled"
	strPwdExpires = "Password Expires"
	Set maxPwdAge = objDomain.maxPwdAge
	wscript.echo objUser.maxPwdAge
	numDays = CCur((maxPwdAge.HighPart * 2 ^ 32) + maxPwdAge.LowPart) / CCur(-864000000000)
 
	strName = objRecordSet.Fields("Name").Value
	strPwdLastSet = objUser.PasswordLastChanged
	strAccExpDate = objUser.AccountExpirationDate
	strLastLogin = objUser.LastLogin
	strPwdExpirationDate = DateAdd("d", numDays, objUser.PasswordLastChanged)
	intUAC = objRecordset.Fields("userAccountControl").Value
	boolAccountDisabled = objUser.AccountDisabled
 
 
	'If (intUAC AND ADS_UF_ACCOUNTDISABLE) Then
		'strAccountDisabled = "Account Disabled"
	'End If
 
	If boolAccountDisabled Then
		strAccountDisabled = "Account Disabled"
	Else
		strAccountDisabled = "Account Enabled"
	End If
 
	If (intUAC AND ADS_UF_DONT_EXPIRE_PASSWD) Then
		strPwdExpires = "Password Never Expires"
	End If
 
	Wscript.Echo strName & vbTab & strAccountDisabled & vbTab & strAccExpDate & vbTab & strPwdExpires & vbTab & strPwdExpirationDate & vbTab & strPwdLastSet & vbTab & strLastLogin
 
	objRecordSet.MoveNext
Loop

Print This Email this Twit This! Add to del.icio.us Share on Facebook Digg This! Stumble It! AddThis! Share on Segnalo Alice Share on OKNotizie

Post Metadata

Data
28 Aprile 2010

Autore
Mirko

Categorie

1 commenti a “List All Users Last Login, Account And Password Expiration Details”




  1. Grazie mille!!! Era propio quello che stavo cercando!!
    Funziona alla perfezione!

    Rispondi



Lascia un commento