Code Retrofitted to :
Code:
$ldap="LDAP://corp/OU=CLN Computers, OU=CLN-Clinton, OU=Bearing, DC=corp, DC=timken, DC=com"
$Computers = GetObject($Ldap)
If @ERROR = 0
;For Each $Computer in $Computers
; ? Right($Computer.Name,Len($Computer.Name)-3)
;Next
;Get $x
;If $x = "q"
; Exit
;EndIf
WriteLog($LogFileName, "script started")
For Each $Computer in $Computers
$ComputerName = Right($Computer.Name,Len($Computer.Name)-3)
? "Processing $ComputerName"
$result = RedirectOutput($OutFileName)
? "Processing $ComputerName"
$result = RedirectOutput("")
If (UserInLocalGroup($ComputerName, "Users" , "Domain Users") = 'True')
RemoveUserFromGroup($computerName, "Users", "Corp\Domain Users")
EndIf
Next
WriteLog($LogFileName, "Script completed")
Else
$str = "Error(" + @ERROR + "(" + @SERROR) + ")) returning computers from DOMAIN(CORP)"
? $Str
WriteLog($LogFileName, $str)
EndIf
Function UserInLocalGroup($ComputerName, $Group, $User)
$GroupObj = GetObject("WinNT://" + $ComputerName + "/" + $Group)
If @ERROR = 0
$FlagFound = 'False';
For Each $UserObjG IN $GroupObj.Members
If $UserObjG.Name = $User
$FlagFound = 'True'
EndIf
Next
$UserInLocalGroup = $FlagFound
Else
$str = "Error(" + @ERROR + "(" + @SERROR + ")) returning Local Group(" + $Group + ") for Computer(" + $ComputerName + ")"
? $Str
WriteLog($LogFileName, $str)
EndIf
EndFunction
Function RemoveUserFromGroup($Computer, $Group, $User)
; Log the fact this machine has the group in its list and then remove it from the list
WriteLog($LogFileName, "$Computer contains " + Chr(34) + $User + Chr(34) + " in " + chr(34) + $Group + Chr(34))
$ShellStr = "C:\grpmaint.exe --Sam \\" + $Computer + " --remove --name $Group --member " + Chr(34) + "$User" + Chr(34) + " --logfile " + Chr(34) + "$LogFileName" + Chr(34)
Shell $ShellStr
Exit @ERROR
EndFunction
Function AddUserToLocalGroup($Computer, $Group, $User)
; Ranamed from GroupAdd (http://www.kixtart.org/ubbthreads/showflat.php?Number=81735)
; Variables Modified $Target = $Computer, $ObjToAdd = $User
DIM $computer, $group, $User, $obj, $objhome
If SubSTR($computer,1,2) = '\\'
$computer = SubSTR($computer,3,Len($computer))
EndIf
If InSTR($User,'\') <> 0
$User = Split($User,'\')
If UBound($User) <> 1 Exit 13 EndIf
$objhome = $User[0]
$obj = $User[1]
Else
$objhome = $computer
$obj = $User
EndIf
$group = GetObject('WinNT://' + $computer + '/' + $group)
If VarType($group) = 9 and @ERROR = 0
$group.Add ('WinNT://'+$objhome+'/'+$obj)
Else
$GroupAdd = @ERROR
Exit @ERROR
EndIf
$GroupAdd = @ERROR
Exit @ERROR
EndFunction
Function WriteLog ($LogFile, $LogString)
If Open(1, $LogFile, 5) = 0
$LogString = "@MONTHNO.@MDayNo.@Year @TIME " + $LogString + @CRLF
$result = WriteLine (1, $LogString)
Close(1)
Else
BEEP
? "Failed to open Log file(Error[" + @Error + "]) "
Exit @ERROR
EndIf
EndFunction
I am still experiencing randomly located errors. Have I missed a point to trap an error? Any further ideas?