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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| ' Script de récupération du dernier logon supérieur à 60 jours
' MP le 26/07/07
' Utilise la propriété LastLogonTimestamp (répliquée sur les DC)
' Renvoie Never si jamais connecté
' La valeur du nombre de jour d'expiration est à paramétrer ligne 66 : If DaysValue >= 60
Const ForAppending = 8
Domain="integr"
strDC ="ms-integr05"
On Error Resume Next
date2 = Replace(Date, "/", "-")
'wscript.echo date2
Result_file = "C:\UserExpired-60-" & date2 & ".csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(Result_file)
objFile.close
Set objTextFile = objFSO.OpenTextFile(Result_file, ForAppending, True)
Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 600
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1
Do While (objExplorer.Busy)
Loop
Set objDocument = objExplorer.Document
objDocument.Open
objDocument.Writeln "<html><head><title>Audit des comptes</title></head>"
objDocument.Writeln "<body bgcolor='white'>"
Set oDomain = GetObject("WinNT://" & Domain)
oDomain.Filter = Array("User")
objDocument.Writeln "Derniere connexion des comptes au domaine " & oDomain.Name & "<br>"
objTextFile.write "Login;Nom;Direction;DN;Dernier Login;Nombre de jours;A désactiver;Commentaire" & vbCRLF
Set oRootDSE = GetObject("LDAP://RootDSE")
Set oShell = WScript.CreateObject("WScript.Shell")
For Each oDItem In oDomain
'wscript.echo oDItem.name
Err.Clear
sUserName = oDItem.name
Set oUser = GetObject("LDAP://" & GetoUser(sUserName))
DNUser = oUser.distinguishedName
'On ignore les comptes pour Citrix
if lcase(instr(1, DNUser, "Architecture Citrix", 1)) = 0 then
'On ignore les comptes déjà désactivés
if oUser.AccountDisabled = FALSE then
Set objLastLogon = oUser.Get("lastLogonTimestamp")
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
'Wscript.Echo "User: " & sUserName & " Last logon time: " & intLastLogonTime + #1/1/1601#
If Err.Number = 0 Then
realLastLogonTime = intLastLogonTime + #1/1/1601#
LastLogonDate = Split(realLastLogonTime)
'Wscript.Echo "Date : " & LastLogonDate(0)
DaysValue = DateDiff("d", LastLogonDate(0), Date)
OUDirect = split(DNUser, ",", -1, 1)
Direction = split(OUDirect(1), "=", -1, 1)
'Wscript.Echo "Jours : " & DaysValue
If DaysValue >= 60 Then objTextFile.write sUserName & ";" & oUser.CN & ";" & Direction(1) &";" & oUser.distinguishedName & ";" & realLastLogonTime & ";" & DaysValue & ";true;Désactivé le " & Date & " par ARS" & vbCRLF
End If
End If
End If
objDocument.Writeln sUserName & " audité" & "<br>"
Next
objTextFile.close
liste.close
'############################################################
' Fonction GetoUser
Function GetoUser(ByVal LogonAccount)
Const ADS_PROPERTY_CLEAR = 1
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Determine le nom DNS du domain depuis l'objet RootDSE.
strDNSDomain = oRootDSE.Get("defaultNamingContext")
' utilise l'objet NameTranslate pour trouver le nom netbios du domaine depuis le nom dns
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' suprime le dernier antislash
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
strUserNTName = Trim(LogonAccount)
' utilise l'objet NameTranslate pour convertire le nom NT en Chaine LDAP.
'On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strUserNTName
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "ERROR: Login not found in Active Directory: " & strUserNTName
Else
On Error GoTo 0
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
'On Error Resume Next
GetoUser = strUserDN
End If
End Function
'################################################################# |
Partager