keybrdcowboy
IS-IT--Management
I keep getting a '(null)' error in the following script. It used to work, but ever since we did a migration and the LDAP 'name' field changed, I am having all sorts of problems.
Here's the code:
Okay, the function at the bottom returns the Distinguished Name just fine. I can WScript.Echo that and it shows up no problem. The error comes on this line "Set UserObject = GetObject("LDAP://" & strUserDN & "")". I keep gettnig a null error. Can anyone help me out with that? Thanks.
Here's the code:
Code:
Option Explicit
Dim oFSO, readFile, outFile, iUpperBound, strUserDN, strUser, oConnection, oCommand, oRecordSet, oRootDSE, strBadCount, strGoodCount
Dim strSameCount, strCheckDisable, strOutFilePath, strReadFilePath, user, UserObject, strTotalCount, WshShell, strFirstName
Dim strLastName, strCity
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Const ADS_UF_ACCOUNTDISABLE = 2
Const ForAppending = 8
' The following variables are used as counters to track how many accounts are modified
strGoodCount = 0
strBadCount = 0
strSameCount = 0
strTotalCount = 0
' Path that sets the log file path for the script. This command will create the file if it doesn't already exist, or append to an
' existing file. Please refer to this log file to check on the script's progress.
'strOutFilePath = "c:\documents and settings\user\desktop\testing.txt"
strOutFilePath = "c:\testing.txt"
' Path that sets the input file for the script. This file contains the usernames that you want to disable. The file must be in a
' single column with just the usernames.
strReadFilePath = "c:\documents and settings\user\desktop\bigtest.txt"
' This line actually sets the path to the variable specified above.
Set readFile = oFSO.OpenTextFile(strReadFilePath)
' This section of code checks to see if the log file already exisits. If it does, it will append to the existing log file, if not, it
' will create the file. Please check file path above to make sure it is appropriate.
If oFSO.FileExists(strOutFilePath) = True Then
Set outFile = oFSO.OpenTextFile(strOutFilePath, ForAppending)
Else
Set outFile = oFSO.CreateTextFile(strOutFilePath, True)
End If
' The following lines will start off the log file
outFile.WriteLine "******************************************************************************************"
outFile.WriteLine "******************************************************************************************"
outFile.WriteLine "Starting the DisableAccounts Script at " & Time & " on " & Date & "."
outFile.WriteBlankLines(1)
' Opens a textfile containg the usernames you want to pull from exchange 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
' Notifies user the script is starting
WshShell.Popup "Starting DisableAccounts Script...",7,"Starting DisableAccounts Script"
' This is the code that loops through the array and disables the account and forces them to change password at next logon
For Each user in arrUserNames
strUser = Trim(user)
strUserDN = fncConnectAD(strUser)
WScript.Echo struserdn
'On Error Resume Next
Set UserObject = GetObject("LDAP://" & strUserDN & "")
If Err <> 0 Then
outFile.writeLine "There was an error connecting to " & strUser & " " & Err.Num
strBadCount = strBadCount + 1
Else
strLastName = UserObject.get("sn")
strFirstName = UserObject.get("givenName")
strCity = UserObject.get("l")
'strCheckDisable = UserObject.get("userAccountControl")
'If strCheckDisable AND ADS_UF_ACCOUNTDISABLE Then
' strSameCount = strSameCount + 1
' outFile.WriteLine "User account " & strUser & " was already disabled. No action taken. " & Date & _
' " at " & Time
'Else
' UserObject.Put "userAccountControl", ADS_UF_ACCOUNTDISABLE
' UserObject.Put "pwdLastSet", 0
' UserObject.SetInfo
' outFile.writeLine "User account " & strUser & " was disabled and set to change password at next logon on " & Date & _
' " at " & Time
' strGoodCount = strGoodCount + 1
'End If
WScript.Echo strLastName
WScript.Echo strFirstName
WScript.Echo strCity
End If
'On Error Goto 0
Err.Clear
Next
' Line that adds up all the accounts tried
strTotalCount = strGoodCount + strBadCount + strSameCount
' Some lines to close out the log file with summary information
outFile.WriteLine "__________________________________________________________________________________________"
outFile.WriteBlankLines(1)
outFile.WriteLine "Summary information for the DisableAccounts Script:"
outFile.WriteLine vbTab & "Number of accounts successfully changed: " & strGoodCount
outFile.WriteLine vbTab & "Number of accounts unsuccessfully attempted: " & strBadCount
outFile.WriteLine vbTab & "Number of accounts already disabled: " & strSameCount
outFile.WriteLine vbTab & "Number of total accounts attempted: " & strTotalCount
outFile.WriteBlankLines(1)
outFile.WriteLine "DisableAccounts Script completed at " & Time & " on " & Date & "."
outFile.WriteBlankLines(3)
WScript.Echo "DisableAccounts script completed at " & Time & " on " & Date & "."
' This function returns the complete distinguished name of the user contained in the arrUserNames array
' Returns it to the fncConnectAD variable
Public Function fncConnectAD(strUser)
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=" & _
strUser & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
fncConnectAD = oRecordSet.Fields("distinguishedName")
If Err <> 0 Then
outFile.WriteLine "There was an error getting the DN for " & strUser & "." & " " & Err.Num
strBadCount = strBadCount + 1
End If
Err.Clear
On Error Goto 0
End Function
Okay, the function at the bottom returns the Distinguished Name just fine. I can WScript.Echo that and it shows up no problem. The error comes on this line "Set UserObject = GetObject("LDAP://" & strUserDN & "")". I keep gettnig a null error. Can anyone help me out with that? Thanks.