需要VBScript列出已禁用的帐户

时间:2018-06-12 15:46:50

标签: vbscript

以下脚本检查本地计算机是否处于非活动状态(90天或更长时间)帐户并禁用它们。该脚本工作正常,但每次执行时都会列出所有已禁用的帐户。我只需要它列出当天被执行时被禁用的帐户。

Option Explicit
Dim objNetwork, strComputer, objComputer, objUser, dtmLast, objGroup, ObjGroupDict
Dim FSO, OutPutFile

Set FSO = CreateObject("Scripting.FileSystemObject")

'1 = reading, 2 = writing, and 8 = appending. The third parameter is a
'boolean true means a new file can be created if it doesn't exist. False
'means a new file cannot be created.
Set OutPutFile = FSO.OpenTextFile("C:\Test\Result.log", 8, True)

'Bind to the local computer.
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")

'Enumerate all users.
objComputer.Filter = Array("user")
For Each objUser In objComputer
    Set ObjGroupDict = CreateMemberOfObject(objUser)

    If ((Left(objUser.Name,3) <> "au_") And (CBool(ObjGroupDict.Exists("Administrators") = False))) Then
        'Trap error if user never logged in.
        On  Error Resume Next
        dtmLast = objUser.lastLogin
        If (Err.Number <> 0) Then
            dtmLast = #1/1/1970#
        End If

        On  Error GoTo 0

        'Check if last logon was more than 90 days in the past.
        If (DateDiff("d", dtmLast, Now()) > 90) Then
            'Disable the user.
            objUser.AccountDisabled = True
            objUser.SetInfo

            OutPutFile.WriteLine(Now & " " & strComputer & " " & Wscript.ScriptName & " " & objUser.Name & " " & "Disabled")
            'MsgBox objUser.Name

            Set FSO = Nothing
        End If
    End If
Next

Function CreateMemberOfObject(objUser)
    'Given a domain name and username, returns a Dictionary
    'object of groups to which the user is a member of.
    'Inputs: objUser - User Object

    Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
    CreateMemberOfObject.CompareMode = vbTextCompare
    Dim objGroup
    For Each objGroup In objUser.Groups
        CreateMemberOfObject.Add objGroup.Name, "-"
    Next
End Function

1 个答案:

答案 0 :(得分:1)

即使帐户已被停用,您也会停用上次登录超过90天的所有帐户。添加仅匹配未禁用的帐户的条件,代码将执行您想要的操作:

If DateDiff("d", dtmLast, Now) > 90 And Not objUser.AccountDisabled Then
    ...
End If