VBScript 禁用 Active Directory 中旧帐户






3.30/5 (7投票s)
在指定的组织单位 (OU) 中搜索所有未在指定时间内登录的用户。然后,您可以选择禁用他们并将他们移动到新的文件夹。
引言
此脚本主要用于搜索并禁用过期的账户。代码相当简单,但使用了 LDAP、WinNT 和 FSO 的组合来实现其目标。 附件是一个可运行的脚本,应在 AD 服务器上以管理员身份运行。 您只需要在顶部的变量声明中输入您的域信息。 它将显示一条消息,询问您是否要禁用这些帐户,以及另一条消息,询问您是否只想将输出保存到文件。
背景
如果您的业务像我的业务一样,人力资源部门永远不会告诉您某人已经离职,因此每月运行此脚本至少可以告诉您他们上次登录的时间。
代码
此脚本的主要功能基于 ADSI,并使用 LDAP 对象查询 Active Directory。 由于 LDAP 查询只能访问单个组织单位 (OU),因此您必须递归搜索所有子文件夹才能找到所有用户。
首先,您需要根据您的 AD 设置一些变量。
bDisable = 0
'do you want to disable and move the accounts?
strFileName = "c:\users.tab"
'the file where the tab delimited results are saved
strUserDN = "servername/OU=All Users, dc=yourdomain, dc=com"
'initial OU where the users are located
'you can leave out the servername/ if you only have 1 domain controller
strNewParentDN = "OU=Inactive Users, dc=yourdomain, dc=com"
'location where disabled users are moved to
strDomain = "yourdomain.com"
'FQDN
iDayThreshold = 180
'number of days without logging in
这两个简单的函数可以递归查找所有用户。
Function EnumOUs(sADsPath)
'recursively finds all of the OU's and users in the given AD path
Set oContainer = GetObject(sADsPath)
oContainer.Filter = Array("OrganizationalUnit")
For Each oOU in oContainer
EnumUsers(oOU.ADsPath)
EnumOUs(oOU.ADsPath)
Next
End Function
Function EnumUsers(sADsPath)
'finds all of the users' last login time
Set oContainer = GetObject(sADsPath)
oContainer.Filter = Array("User")
For Each oADobject in oContainer
strOut = strOut & oADobject.Name & vbCrLf
'you can put other things here depending on what you want to do
Next
End Function
这将基本上构建一个 string
,其中包含所有用户。 但是,与其只构建一个 string
,我们还可以获取每个用户的 lastLogon
属性。 一旦我们有了它,我们就可以确定如何处理在给定时间范围内未登录的用户。
由于 lastLogon
属性在 LDAP 中保存为整数,因此您必须将数据作为对象收集并将其转换为可用的日期值。
'for each user object, oADobject find the last logon
Set objLogon = oADobject.Get("lastLogon")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
intLogonTime = intLogonTime + #1/1/1601#
inactiveDays = Fix(Now() - intLogonTime)
基于您选择的逻辑,您可以禁用帐户或将它们移动到“非活动用户”文件夹,或者两者都做。 此函数将移动用户,然后禁用它。
Sub MoveUser(adsName, adsPath, adsSAM)
'adsName is the CN of the object - CN=Some Guy
'adsPath is the full DN path - LDAP://cn=Some Guy,
'OU=All Users, DC=yourdomain, DC=com
'adsSAM is the unique object name (their username) - someguy
'moves the user from the given OU to a new OU
Set objUser = GetObject("LDAP://" & strNewParentDN)
objUser.MoveHere sPath, sName
'then disable the user
Set objUser = GetObject("WinNT://" & strDomain & "/" & _
oADobject.sAMAccountName)
objUser.AccountDisabled = True
objUser.SetInfo
End Sub
然后,我们还可以使用 FSO 将禁用用户的列表保存到文件中(如果您需要)。 此函数接收输出字符串并将其保存到文件。
Sub SaveToFile(strData)
'create a FSO
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'if the file exists already open it for writing
If objFSO.FileExists(strFileName) Then
Set objTextStream = objFSO.OpenTextFile(strFileName, 2)
objTextStream.Write strData
objTextStream.Close
Set objTextStream = Nothing
'otherwise, create the file and write the data
Else
Set objTextStream = objFSO.CreateTextFile(strFileName, True)
objTextStream.Write strData
objTextStream.Close
Set objTextStream = Nothing
End If
End Sub
下载脚本的完整副本 此处。
关注点
我在不同的网站上找到了此脚本的各个部分,但从未找到任何将它们全部联系起来的东西。 这些例程的组合确实为系统管理员提供了一些很好的功能,可以删除不活动用户并生成报告。