查看完整版本: 求个脚本,列出90天AD里没登录的用户.

ekingg 2008-4-8 10:20

求个脚本,列出90天AD里没登录的用户.

求个脚本,列出90天AD里没登录的用户,并删除.*L{6N-OT DF
1dK j`%Y/T o
顺便问下有什么可以加密VBS的内容的工具或算法.&f4u)Q4`%pzqiL

h T4L!j$boTi [[i] 本帖最后由 ekingg 于 2008-4-8 11:06 编辑 [/i]]

ekingg 2008-4-8 17:00

高手今天都很忙吗
8Em S"F U 偶把现在查找的脚本放上来给大家看看.   :P
G([IC\] Vk/J'R.Tx,~G
'Modified for US Date format  m/d/y, file name ? still a problem_ r~#qsN I
'Modified for disabled new account Notlogon and disabled >= 90 days3Xv+H5W h oo%L)MK W4}
'Modified for new account not logon >= 30 days and not disabledF9ya rpb-RW8|,a&t

!B H9Y9i#[dO2GhT 'ODD Users: disabled but not listed, sovled by using "m/d/y" format for condition^3L2s$dIVr0e
ik o.|w V.l+f6m8TB v
'********************
B{ G3b@j9x '* Define Constants **c^$Br%p(dX9Z5A
'********************
?`0~w A Const MgmtDel  = -90  'disabled or not logon for XX days, old account!
!?5qx!HrJ-d,I/A:sN Const MgmtDis  = -30                'not logon for XX days, new account!Vk7hdkm
Const ADS_PROPERTY_APPEND = 3
H'g4NT,Sp(bc Const ADS_NAME_TYPE_NT4 = 3
9E _$p[2h{6@j2sTS Const ADS_NAME_TYPE_1779 = 1
Y|1NRRBZ
A } ^H1T!Bp '*********************
@)tw!^ z)d k U '* Declare Variables * V+wO`9n_
'*********************j0ig)I2fii|
Dim expDate 'expiration date4uF:[:@w6U4Rg&M
Dim createDate 'account first created
Wb j3D;h @0fk Dim lastLogDate 'account last logon dateG%CZ`'a
Dim lastmodDate 'last modified date
6DS!y5@W Dim accDisabled 'if account disabled, 514 for disable, 512 for enable(e e PN*{o$D
Dim mFile
$J-V H&Y,Uw Dim WshShell [)N }4QJ#jY L
Dim strFilter.R&T7O}1D\Om6J~
Dim strAttributes
8I)T'c0r ]5w9c Dim strQuery
9J,d,]ZOx6i Dim objRootDSE
5r(k.znJ:V Dim strDNSDomain;@-S9W |%O;D
Dim objCommand4gW;r(\{B|o5?
Dim objConnection
j!Sm-P:n7~%JO ? Dim MyArr
Mr*[1Q(z9mf Dim Upper
-V*g$W7}:wS Dim strUserNTName
h\3C8y N Dim objTrans
&?+Yk8sfg {x Dim strNetBiOSDomain u9V9RNG$A-iYh
Dim strUserDNZd-W3n%u)g2R
Dim objDate|2im!{ wv

B`,|u!O Owd:V '*********************yOjEWTm N)L!l
'* Get Required Info *(T,Kv|6f/K3\%g%T @:qOF
'* and Build Outputs *
-K;a+X F_Rf '*********************
?q%w \~T ' Determine DNS domain name.tj_{l~"}
Set objRootDSE = GetObject("LDAP://RootDSE")
3h y ld[L#H strDNSDomain = objRootDSE.Get("defaultNamingContext")                        'domain DNS name
(\%E1Zo.\4a strNetBiOSDomain = "ASIA_RRD_COM"                 'domain NetBIOS name0R xDl d;HZ)^
Set fso = CreateObject("Scripting.FileSystemObject")7t4N!G |y#fMDH _%B
Set mFile = fso.CreateTextFile("./" & strNetBiOSDomain & "-UserManage-" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & ".txt") 5yL:~'K]Ai c
mFile.Writeline("UserName,AccountType,SinceCreated,SinceLastModified,SinceLastLogon,Actions")
\jT&b3t9G_.J '******************
#]Q0Y5s1`Xs8RU| '* Search the Domain * SHOULD USE ADO LDAP to support timestamp attr
&D W\)M+_"qn:r"Q '****************** d9\F.^*N
' Use ADO to search Active Directory.
8i:kr3R,q:w J6K5T                         Set objCommand = CreateObject("ADODB.Command")o|f1G-h^8M6}
                        Set objConnection = CreateObject("ADODB.Connection")Kr$j!Yd)x5D/d
                        objConnection.Provider = "ADsDSOObject"
#waIE+V                         objConnection.Open "Active Directory Provider"~2x!JvYt i
                        objCommand.ActiveConnection = objConnection6AG1b E8ES
                        strBase = "<LDAP://" & strDNSDomain & ">"
/|\$E2q$l-z                         strFilter = "(&(objectCategory=person)(objectClass=user))"
4p(U;?C ~                         strAttributes = "givenName,sn,sAMAccountName,name,displayName,userPrincipalName,whenCreated,lastLogon,whenChanged,userAccountControl"
"D'f5x!g9m4O.H3gbQ                         strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"] U'DY}d)y
                        objCommand.CommandText = strQueryT5{1Wa Y){ x:} C;I
                        objCommand.Properties("Page Size") = 20
6y$M/y:|(L _r                         objCommand.Properties("Timeout") = 30
0}C0XiY#PH;rm+E                         objCommand.Properties("Cache Results") = False8r8_&A5V0U6up"x_
                        objCommand.Properties("Sort On") = "sAMAccountName"
%Y%ZBk&Y@+h6^3B yh                         Set objRecordSet = objCommand.Executeq1[jK {9b
                        Set objRecordSetArr = objCommand.Execute!HS{K5Y.J^

w@ d~3q                         If objRecordSet.EOF Then
GV k`!r1l}f                                         Wscript.Echo "No user found!!".hM+iy[:kDlqH
                        End If
:yWJ X3AD                         7a_O7RK
                        'if user exists, then run search
!m5_uz1kt4JC7r                         '****************************
)foT&Gi;Le|                         '* Start Loop to find Users *9h9w"M])[z
                        '*****************************V4eq1A${ h#c\2X
                        Do Until objRecordSet.EOF
@*v$x;c E'u[}(}.J,W _!_qB:X)s
                                                                        strUserNTName = objRecordSet.Fields("sAMAccountName").Value
!I$TC;g_                                                 J5}i6{-cH/C|
                                                                        'Wscript.echo strUserNTName(l{!G j}h;Y)L q
                                                                        Set objTrans = CreateObject("NameTranslate")
:c$u~-Yp&H/]&t&mq v                                                                         objTrans.Set ADS_NAME_TYPE_NT4, strNetBiOSDomain & "\" & strUserNTName+c"r:X P7d B
                                               
\A'Y6E/{9d,Bj                                                                         strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
.}vQ1|2|W
+~%tD'?pQS~[i                                                                         ' Bind to the user object in Active Directory with the LDAP provider.+\T!Qt^ U-\
                                                                        'On Error Resume Nextg `"b-g[s)X
                                                                        If InStr(strUserDN,"SystemMailbox") = False Then $f;N%F@ J
                                                                                'MsgBox "bingo!"
2o_k6Qxm                                                                                 Set objUser = GetObject("LDAP://" & strUserDN)c Y$g7j s3n hdYg
                                                                                objUserNULL = 0                'False for non-exchange systemMailbox account5S"|/Y9oKu
                                                                        ElseY/\O.Nk9\
                                                                                 objUserNULL = 1                'true for exchange systemMailbox account
WvW ^2{:z1B|0C                                                                         End if
C-X,iLa0R*M                                                                         :d9Z^"H g$n t
                                                                        'Reset counter valuesqZ|ks&A6x
                                                                        newAccountCreate = 02E%qf#`#|
                                                                        accountLastLogon = 0"A'Q @4Gc
                                                                        accountLastMod = 00MyB?cl0sS
                                                                        accDisabled = 0
y e0K\+`t jN                                                                         %j)K'^)].\6p$|4O
                                                                        '************************************
ZlU}n                                                                         '* Calculate the difference between *
^;O_f&W-cf t                                                                         '* now and the expiration date.  date value xxxx-xx-xx can only be used with date functions!!! or converted by Integer8Date()   *O S0b ls ^7t Z
                                                                        '************************************
-jg7gL9Iyu.A                                                                         'MsgBox strUserDN
4u i)k O k0fxd\                                                                         If objUserNULL = 0 Then                        'not a exchange system account x$C `!F4R)Z!Vh
                                                                                /x!|eSD!BA#Ny
                                                                                'Wscript.Echo objUser.lastLogontOG0m+L*j)Ki#P
                                                                                #OA SE Mg2k
                                                                        accDisabled = objUser.userAccountControl                                                                                                'enabled(512)? disabled(514)?password never expire(66048)EO/Dh:K.\M-gg
                                                                        If IsEmpty(objUser.lastLogon) = False Then                                                                                         'check if objUser.lastLogon exists, can be NOT Set,0,large integer
#R&y{(ags Q                                                                                 lastLogDate = Integer8Date(objUser.lastLogon, lngBias)                                                'lastlogon timestamp, converted to normal DateDk5g8o H C
                                                                        Else
j:`;K0n'y-L}U                                                                                 lastLogDate = "1601-1-1"                                                                                                                                                ' for Windows 2003 SP1 AD, lastLogon should be NULL for new userv0p4vv*Gi
                                                                        End Ify w$f} t~ tG U
                                                                        %q/L9o#q~
                                                                        'msgbox lastLogDate |~1uUp L
                                                                        newAccountCreate = DateDiff("d",now,objUser.whenCreated)                                'since first created, days?
jVb LLN                                                                         9sA L"l5r7s)N
                                                                        If CStr(lastLogDate) <> "1601-1-1" And CStr(lastLogDate) <> "1/1/1601" Then                                                         'new user last logon time should be like 1601-1-1
y NA)I*`I#av                                                                                 accountLastLogon = DateDiff("d",now,lastLogDate)                'since last logon, days? CAN BE 0 DAYS;E6dz^w"Z!bZ
                                                                        Else$el{} sb_4hj^
                                                                                accountLastLogon = -36500                'Never logon to AD, should not be longer than 100 years
;K?)dZh"M                                                                         End Ifs6zpU-d`j8t
                                                                       
:cc j3|CH!OW                                                                         accountLastMod = DateDiff("d",now,objUser.whenChanged)                                                                                                        'since last changed, days?
[ w~\O"CJG                                                                 Ht3h ] F G!K(r#{
                                                'MsgBox Objuser.sAMAccountName&"----"&newAccountCreate & "--" & accountLastMod & "--" & accountLastLogon
9xE1C"Y:c F                                                
%v8Q8sP#j3u                                                                                 '******************
.v8WM#on8RA                                                                                 '* Newly Created users (exists 30days) that already be disabled (and not modified for 30 days) should be deleted, >30 days*
/b4v:t/Bl}D}                                                                                 '******************
$F+lpgy4s                                                                         If (newAccountCreate <= MgmtDis And accountLastMod <= MgmtDis And (CStr(lastLogDate) = "1601-1-1" Or CStr(lastLogDate) = "1/1/1601") And (accDisabled = 514 Or accDisabled = 66050)) Then
agoi$ZqL#D                                                                                 mFile.Writeline(Objuser.sAMAccountName & ", [[Disabled NEW Account]," & newAccountCreate*(-1) & "," & accountLastMod*(-1) & "," & accountLastLogon*(-1) & ", Account Should be deleted!!")
E9j0] gv JtO*Zz                                                                                 'MsgBox Objuser.sAMAccountName & ", [Disabled NEW Account], Account Should be deleted!!"
!K'vv!rb%D*]                                                                         End If
ZTzj;?l9Zs
O{m#m"KdQ,m                                                                                 '******************Cq3L3^(`
                                                                                '* Newly Created users that should be disabled, >30 days not logon*
%~ b5Z0Ga R3E.bS                                                                                 '******************
;Q4x q+HID1s2E'O'D                                                                         If (newAccountCreate <= MgmtDis And (CStr(lastLogDate) = "1601-1-1" Or CStr(lastLogDate) = "1/1/1601") And (accDisabled <> 514 And accDisabled <> 66050)) Then
"W:^bbH qwBz                                                                                 mFile.Writeline(Objuser.sAMAccountName & ", [NEW Account]," & newAccountCreate*(-1) & "," & accountLastMod*(-1) & "," & accountLastLogon*(-1) & ", Account Should be disabled!!")
6^t&}5l|?:q'i                                                                                 'MsgBox Objuser.sAMAccountName & ", [NEW Account], Account Should be disabled!!"
3hm1|K/}6y'M-i_                                                                         End If
MX q/r+zC*U ?3@O!B;`[                                                                        
5Mi8qGc1gjns;W                                                                         If accountLastLogon <> -36500 And accountLastLogon <> -148042 ThenzT NSN(jZe|
                                                                                                                '******************}e&Sk4M#X[l
                                                                                                                '*users not used that should be disabled, >90 days not logon*E+\;a~hoTgU
                                                                                                                '******************
r"\m g,sj                                                                                                         If (accountLastLogon <= MgmtDel And (accDisabled = 512 Or accDisabled = 66048)) Then                                'when pwd never expire, 66048
xU!_:|3?                                                                                                                 mFile.Writeline(objUser.sAMAccountName & ", [Old Account]," & newAccountCreate*(-1) & "," & accountLastMod*(-1) & "," & accountLastLogon*(-1) & ", Account Should be disabled!!")?0@QG o,C#~1yB5{
                                                                                                                objUser.AccountDisabled = TrueG a Q&wqo
                                                                                                                'MsgBox objUser.sAMAccountName & ", [Old Account], Account Should be disabled!!"2yrdg)i0tH:k
                                                                                                        End IfGM!toB#NVh3Z
                                                                                                        7Au%eb Z,X
                                                                                                                '****************** k RwG] IM%W
                                                                                                                '* users disabled that should be deleted, >90 days not enabled again*
M n,b @ Vp                                                                                                                 '******************!X0us*]9~O:Xd
                                                                                                        If (accountLastMod <= MgmtDel And (accDisabled = 514 Or accDisabled = 66050)) Then                                'when pwd never expire, 66050,xKh+{^"c i$H#u
                                                                                                                mFile.Writeline(objUser.sAMAccountName & ", [Disabled Account]," & newAccountCreate*(-1) & "," & accountLastMod*(-1) & "," & accountLastLogon*(-1) & ", Account Should be Deleted!!")
e\Oo F](Y!J                                                                                                                 objUser.AccountDisabled = Truep/eJ9SUeR;U3Z
                                                                                                                'MsgBox objUser.sAMAccountName & ", [Disabled Account], Account Should be Deleted!!"
KV%W my7Z*M2N [S$\                                                                                                         End IfK oQv~d0pWyeO
                                                                                                        5N4Y%pu n6D#LZk^sG

`U ~gL;X)I D V#LR                                                                         End If5ZA d1b [8hW{t|/s
                                                                        End Ifu.F)V.@'p3QN?I s+T*V
                                                                       
"](k5z-Wn!X                                                                 '        If accountLastMod = -945 Then@ [ @/K(_STe
                                                                '                objUser.AccountDisabled = True
:Pj\!N-R{e                                                                 '                objUser.SetInfo
s'MffCbvY'R:j(~                                                                 '        End If
b#a&N.go+^c`m_                                                                        
dWo1Yn3Xh^                         objRecordSet.Movenext
&B[,i0SX ~$Z                         Loop
|:gB*G7k%}]w 'Next\'rt`aF+u@&q0]

pl4a/gII&o '******************************************
Vd||Zz z(V&q*nyh '* Clean up open files and send log files *
+].J+vSmT;f '******************************************}p GnVZ#ui^
'*******************
R(k/y+zEh0I '* Close all Files *AD,NNk&P1XCb
'*******************(xwXh!H;N.E
mFile.close
3M5py(o%Y3H"fF~,Q;l )q Eo:c1Zi7[|X CP
'****************s-s$s:eJn^\!s
'* Delete Files *)S6O(r|1bM8pz;nN
'****************`b5|rx3?K:s'Dka
'fso.DeleteFile(mFile)
9C W-_\8W V tXd-G 'fso.DeleteFile(uFile);{5ar v#aT2y
'fso.DeleteFile(wFile)
(^&nBP ` '*******************
t o#l$T,Nxi '* Quit the Script *%MgoGG4Q aq!}9p
'*******************
0OO2[3N6c8J MsgBox "Searching for invalid users done!!"#Z1`yP(IR
WScript.Quit(1)
c'rMC.B
W^Sgw {H l-L J tW'jM
'*******************a9sQ:u J$}W$\)c
'Functions
U5TeD&~&j*bV1[*^ '*******************
$ob0I XrY Function Integer8Date(objDate, lngBias)
k ^ bg)D ' Function to convert Integer8 (64-bit) value to a date, adjusted for
;p/I3nK``H ' local time zone bias.
:o j Qq;D VAO   Dim lngAdjust, lngDate, lngHigh, lngLow
E$o^N#X3Y@8Q"V8S   lngAdjust = lngBias#t!iH/C,oI?"m5}E3G
  lngHigh = objDate.HighPart
7hQQk pN)v8M   lngLow = objdate.LowPart
9p(f]y_:v ' Account for bug in IADslargeInteger property methods.
^!IH4\kP4{*vf   If lngLow < 0 Then
%Vv#wjv E2E^4U^t     lngHigh = lngHigh + 1+dlL"ue
  End Ifk-VL#R l+zZ3}
  If (lngHigh = 0) And (lngLow = 0) Then5G*J.FNt]#j x
    lngAdjust = 0
p hc M2t&O^X   End If
"v|f]5V8r2`/F   lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
8{nw,tB     + lngLow) / 600000000 - lngAdjust) / 1440
5gj|!k7^/v7J   Integer8Date = CDate(lngDate)
S ~;` kB+r End Function

风中叶雨 2008-4-9 11:11

有高手吗?本人对这个也有兴趣,想找一个禁用90天未登录的AD帐号的脚本,或者命令

ekingg 2008-4-9 14:00

命令就用dsquery user -inactive 10 | dsrm -c -noprompt
$^lyAFG2B7t7t\ $Y8kB:n3r,` y[.BTQ
10表示10周没登录过

594sky 2008-4-10 18:05

楼主能否告知:把您的文章复制到记事本里怎么会出现乱码?本人是菜鸟。d$yiX-GRvn\I
先谢过楼主

594sky 2008-4-16 22:04

好可怜啊,尽然没人回答问题....

ghostshadow 2008-4-17 09:11

这个脚本好多命令 太深奥啦!!:( c,nM*e|0{z
看不懂

风尚 2008-4-29 21:08

[quote]原帖由 [i]594sky[/i] 于 2008-4-10 18:05 发表 [url=http://www.sharecenter.net/redirect.php?goto=findpost&pid=2153462&ptid=209570][img]http://www.sharecenter.net/images/common/back.gif[/img][/url]
#Upmyl)G/B 楼主能否告知:把您的文章复制到记事本里怎么会出现乱码?本人是菜鸟。
mG&t,n }h} 先谢过楼主 [/quote]
@"z;~(WE4Ec 放盗功能k3K5yQP:` Ge
你全选就可以看到其中的奥妙

dj3790 2008-6-8 12:57

用用试试看

cnmdb 2008-6-8 13:10

希望有用

setthyfree 2008-6-16 14:59

ekingg厲害

abcoooo 2008-6-20 20:30

楼主真高,这个脚本非常好用。

adrien0901 2008-8-26 09:50

[quote]原帖由 [i]ekingg[/i] 于 2008-4-8 10:20 发表 [url=http://www.sharecenter.net/redirect.php?goto=findpost&pid=2149632&ptid=209570][img]http://www.sharecenter.net/images/common/back.gif[/img][/url]
;_zh6p)WR 求个脚本,列出90天AD里没登录的用户,并删除.,q?U}Wv(Z |_

V7V1\mg%x;j!{q 顺便问下有什么可以加密VBS的内容的工具或算法. [/quote]
8WYq'@5v -TK4@aa
1:可以将以下代码保存为BAT运行即可
]{ ET Qy 6p$t'k O5l5G
@echo offf/c!F-KF.@ qc t
@dsquery user -inactive 4 > dn.txt
/Ez-^{Hqm @for /f %%1 in (dn.txt) do dsrm %%1 -noprompt
} m"D?2`5{:uI$c,E.v n1dCx`3zw
2:VBS加密工具

lhw_jike 2008-8-26 12:00

这个脚本相当的有用!:lol:
页: [1]
查看完整版本: 求个脚本,列出90天AD里没登录的用户.