Page 1 of 1 1
Topic Options
#152090 - 2005-11-23 05:40 AM Set User Must change pass... from txt file
Scriptodude Offline
Fresh Scripter

Registered: 2005-04-22
Posts: 26
Can anyone tell me how to setup the following script so that it will run faster(a lot faster!)? Script runs through 5000+ user objects.

Thx,
Bryan

Code:
 
Dim $What,$sADsPath,$strFilter
Dim $aResults,$c,$r,$append
OPEN(3,D:\migration\verified.txt,2)
$What = "ADsPath" ;What attributes do we want to use in the fnLDAPQuery function?
$From = "removed"
$sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext") ;Sets variable to root of the domain
$strFilter = "(&(objectCategory=person)(objectClass=user))" ;Sets variable to filter on the class/attribute you want to find in AD!
$aR = fnLDAPQuery($What, $From, $strFilter) ;Start the fnLDAPQuery function using the variables listed above!
$f = READLINE(3)
WHILE @ERROR = 0
For $c = 0 to Ubound($aR)
For $r = 0 to UBound($aR,2)
$user = $aR[$c,$r] ;Set the user variable to the current user object!
$userOBJ = getobject($user)
$logon = $userOBJ.sAMAccountName
IF $logon = $f
$set = UserPass($From, $user) ;Start the UserPass function using the variables listed above!
$set = EnableUser($From, $user)
ENDIF
Next
Next
LOOP
CLOSE(3)


Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
Optional $User,Optional $Pswd)

Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R

$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)

$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf

$oCon.Open("Active Directory Provider")

$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=10000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0

If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf

$aR = $oRS.GetRows()

Dim $aFR[Ubound($aR,2),Ubound($aR,1)]

For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next

$fnLDAPQuery=$aFR
EndFunction

;Function to reset user passwords in AD!
Function UserPass($From, $user)
DIM $computer, $computerOBJ, $userOBJ, $nul
$userOBJ = getobject($user) ;Current user password to reset!
$userOBJ.Put("pwdLastSet",0) ;Put a check in the box that requires user to change password at next logon!
$userOBJ.SetInfo ;Complete the change to the current user object!
IF @ERROR <> 0
$err = @ERROR
$serr = @SERROR
OPEN (1, "%SYSTEMDRIVE%\ADlogs\_SetInfo_Fail.log", 5) ;write log if SetInfo fails!
WRITELINE (1, @DATE + " " + @TIME + " Failed to set info for " + $logon + " with error code " + $err + " " + $serr + @CRLF)
CLOSE(1)
ELSE
OPEN (1, "%SYSTEMDRIVE%\ADlogs\_SetInfo_Success.log", 5) ;write log if SetInfo succeeds!
WRITELINE (1, @DATE + " " + @TIME + " " + $logon + @CRLF)
CLOSE(1)
ENDIF
exit(@error)
Endfunction

;Function to enable user in AD!
Function EnableUser($From, $user)
DIM $computer, $computerOBJ, $userOBJ, $nul
$userOBJ = getobject($user) ;Current user password to reset!
$userOBJ.AccountDisabled = 0 ;Enable the user account!
$userOBJ.SetInfo
IF @ERROR <> 0
$err = @ERROR
$serr = @SERROR
OPEN (1, "%SYSTEMDRIVE%\ADlogs\_EnableUser_Fail.log", 5) ;write log if failure!
WRITELINE (1, @DATE + " " + @TIME + " Failed to enable " + $logon + " with error code " + $err + " " + $serr + @CRLF)
CLOSE(1)
ELSE
OPEN (1, "%SYSTEMDRIVE%\ADlogs\_EnableUser_Success.log", 5) ;write log if success!
WRITELINE (1, @DATE + " " + @TIME + " Enabled " + $logon + @CRLF)
CLOSE(1)
ENDIF
$f = READLINE(3)
exit(@error)
Endfunction

:end
CLOSE(1)
exit


Top
#152091 - 2005-11-24 08:51 PM Re: Set User Must change pass... from txt file
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Bryan, it looks like you're performing a GetObject on each user account at least three times. That is inefficient and will certainly take a while to run against 5000+ user objects.

What is the contents of "D:\migration\verified.txt"?

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 525 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.064 seconds in which 0.037 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org