' This script will enable the user to specify if they want to enable accounts, disable accounts,
' or change the password on selected accounts. The script is setup to use text files already
' created which contain the usernames that need to be modified.
'
Option Explicit
Dim oFSO, readFile, outFile, iUpperBound, strUserName, oRootDSE, oConnection, oCommand, oRecordSet, strConnect, UserObj
Dim strGoodCount, strLogPath, oShell, strEmailLen, strSid, strTotalCount, strOKCount, strEmail, strQuery, strErrorControl
Dim strConfirm, strAction1, strLoopControl, strAction2, strActionSelected, strFileOpen, strAccountStatus, strConfirmPW
Dim strNewPassword
Public strUser
Const ForAppending = 8
Const ADS_UF_ACCOUNTDISABLE = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
strLogPath = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
' Confirmation box asking user to make sure they want to run this script
strConfirm = MsgBox ("Please confirm that you want to run the ResetAccountsSA script. This script will allow you to enable, " & _
" disable, or change the password for selected accounts. The script will prompt you for what action you want to " & _
"take and then ask you to select the usernames that you want to perform that action on." & vbCrLf & vbCrLf & _
"Press OK to continue or CANCEL to quit.",1,"PLEASE CONFIRM")
If strConfirm = 2 Then
WScript.Quit
End If
strLoopControl = 0
Do While strLoopControl = 0
strAction1 = InputBox ("Please type in the action that you want to perform on the selected user accounts." & vbCrLf & vbCrLf & "Enable - " & _
"Enable the selected accounts" & vbCrLf & "Disable - Disable the selected accounts" & vbCrLf & "Reset - Reset the " & _
"passwords on the selected " & vbCrLf & " accounts." & vbCrLf & vbCrLf & "Please click cancel to exit " & _
"the script without making any changes.", "SELECT ACTION")
strAction1 = Trim(strAction1)
strAction1 = Left(strAction1, 1)
If LCase(strAction1) = "r" Then
strActionSelected = "ResetPasswords"
strLoopControl = 1
ElseIf LCase(strAction1) = "e" Then
strActionSelected = "EnableAccounts"
strLoopControl = 1
ElseIf LCase(strAction1) = "d" Then
strActionSelected = "DisableAccounts"
strLoopControl = 1
Else
WScript.Echo "Invalid entry. Please try again."
End If
Loop
Do While strLoopControl = 1
strAction2 = InputBox ("Please type in the number of the group of usernames you would like to make the change to:" & vbCrLf & vbCrLf & _
"1. group1" & vbCrLf & "2. group2" & vbCrLf & "3. group3" & vbCrLf & "4. group4" & vbCrLf & "5. All Users" & _
vbCrLf & vbCrLf & "Please click cancel to exit the script without making any changes.","SELECT USERS")
strAction2 = Trim(strAction2)
If strAction2 = "1" Then
strFileOpen = "testSAscript.txt"
strLoopControl = 0
ElseIf strAction2 = "2" Then
strFileOpen = "345c112xx.txt"
strLoopControl = 0
ElseIf strAction2 = "3" Then
strFileOpen = "345c117xx.txt"
strLoopControl1 = 0
ElseIf strAction2 = "4" Then
strLoopControl = 0
ElseIf strAction2 = "5" Then
strLoopControl = 0
Else
WScript.Echo "Invalid entry. Please try again."
End If
Loop
Set readFile = oFSO.OpenTextFile("c:\inputfiles\" & strFileOpen)
' Opens a textfile containing the usernames you want to pull and puts them in an array
iUpperBound = 0
While Not readFile.AtEndOfStream
ReDim Preserve arrUserNames(iUpperBound)
arrUserNames(UBound(arrUserNames)) = readFile.ReadLine
iUpperBound = iUpperBound + 1
Wend
readFile.Close
If strActionSelected = "DisableAccounts" Then
fncDisableAccounts
ElseIf strActionSelected = "EnableAccounts" Then
fncEnableAccounts
ElseIf strActionSelected = "ResetPasswords" Then
fncResetPasswords
End If
WScript.Echo "Finished with Script"
WScript.Quit
Function fncEnableAccounts
For Each strUser In arrUserNames
strConnect = fncConnectAD(strUser)
strConnect = Replace(strConnect,"/","\/")
Set UserObj = GetObject("LDAP://" & strConnect)
strAccountStatus = UserObj.Get("userAccountControl")
UserObj.Put "userAccountControl", strAccountStatus Xor ADS_UF_ACCOUNTDISABLE
UserObj.SetInfo
Next
End Function
Function fncDisableAccounts
For Each strUser In arrUserNames
strConnect = fncConnectAD(strUser)
strConnect = Replace(strConnect,"/","\/")
Set UserObj = GetObject("LDAP://" & strConnect)
strAccountStatus = UserObj.Get("userAccountControl")
UserObj.Put "userAccountControl", strAccountStatus Or ADS_UF_ACCOUNTDISABLE
UserObj.SetInfo
Next
End Function
Function fncResetPasswords
Do While strLoopControl = 0
strNewPassword = InputBox("Please type in the new password." & vbCrLf & vbCrLf & vbCrLf & vbCrlf & "Please click Cancel to exit the script without making any changes.","SEL" & _
"ECT NEW PASSWORD")
If strNewPassword = "" Then
WScript.Quit
End If
strConfirmPW = MsgBox("The password you entered is: " & strNewPassword & vbCrLf & "Is this correct?" & vbCrLf & vbCrLf & _
"Press OK to continue with resetting the passwords, or press Cancel to retype the new password" & _
".",1,"CONFIRM NEW PASSWORD")
If strConfirmPW = 1 Then
strLoopControl = 1
End If
Loop
For Each strUser In arrUserNames
strConnect = fncConnectAD(strUser)
strConnect = Replace(strConnect,"/","\/")
Set UserObj = GetObject("LDAP://" & strConnect)
strUser = Trim(strUser)
WScript.Echo strUser
WScript.Echo strNewPassword
UserObj.SetPassword strNewPassword
Next
End Function
' This funtion will take a username and return the complete Distinguished Name
Public Function fncConnectAD(strUserName)
'On Error Resume Next
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=User)(sAMAccountName=" & _
strUserName & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
fncConnectAD = oRecordSet("distinguishedName")
'If fncConnectAD = null Then
' outFile.WriteLine strUserName & ",Null DN returned.," & Date & "," & Time
' outFile.WriteLine "Null DN returned. " & Date & " " & Time
'ElseIf Err <> 0 Then
' outFile.WriteLine strUserName & ",Error getting DN.," & Date & "," & Time
'End If
'On Error Goto 0
End Function