#145609 - 2005-08-15 08:07 PM
Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
I have the following script. It seems to be ending prematurely. I added the commented out lines in order to make sure it got all the proper items(which it did). For some reason though it will end with no given error at random locations in the script. Any ideas?
Code:
$ldap="LDAP://corp/OU=CLN Computers, OU=CLN-Clinton, OU=Bearing, DC=corp, DC=timken, DC=com" $Computers = GetObject($Ldap)
;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" If (UserInLocalGroup($ComputerName, "Users" , "Domain Users") = 'True') RemoveUserFromGroup($computerName, "Users", "Corp\Domain Users") EndIf Next WriteLog($LogFileName, "Script completed")
Function UserInLocalGroup($ComputerName, $Group, $User) $GroupObj = GetObject("WinNT://" + $ComputerName + "/" + $Group) $FlagFound = 'False'; For Each $UserObjG IN $GroupObj.Members If $UserObjG.Name = $User $FlagFound = 'True' EndIf Next $UserInLocalGroup = $FlagFound 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 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 + "]) " EndIf EndFunction
I left out the additional documentation comments that was included in GroupAdd for brevity.
|
Top
|
|
|
|
#145610 - 2005-08-15 08:26 PM
Re: Script ending prematurely
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Add error checking after every GetObject. I have seen instances where GetObject fails. Much of your code and the UDFs do not provide the proper error checking to insure the GetObject actually suceeded. If it does not then it should report the error.
After adding this code, let us know if the problem is trapped or if the problem continues. We can then look deeper.
|
Top
|
|
|
|
#145611 - 2005-08-15 08:55 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
I am test running it now. Will let you know the results soon. Thanks for kicking me back in line. I had never looked into COM Error handling, I just depended on the functions I recieved to do so (I know... bad form).
|
Top
|
|
|
|
#145612 - 2005-08-15 10:02 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
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?
|
Top
|
|
|
|
#145613 - 2005-08-15 10:48 PM
Re: Script ending prematurely
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Are you writing your logfile locally? If not, try a local log file to minimize possible network issues within your script.
You speak of random errors. What lines and what errors?
This seems to be part of a larger script. Have you been able to isolate just this code and execute it manually to reproduc the errors?
|
Top
|
|
|
|
#145614 - 2005-08-15 10:52 PM
Re: Script ending prematurely
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
What I am trying to determine is if the code is failing because of a coding problem that only surfaces when certain types or construction of data is processed. To that end add logging for all data returned and sent to functions. This will highlight if the failure occurs when a certain type of string data or object is processed.
Edited by Howard Bullock (2005-08-15 11:32 PM)
|
Top
|
|
|
|
#145615 - 2005-08-15 11:03 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
This is the beginning of a larger script. But for now this is all I have. Random closings of the script window are occurring, not on the same machine each time. When the script closes it is giving no error ( I use RedirectOuput to ensure that if there is an error I would catch it). With no one machine causing the error, and no one (discernable) error line.
|
Top
|
|
|
|
#145616 - 2005-08-16 01:10 AM
Re: Script ending prematurely
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If this is only part of a larger script, how do you know it is ABENDing here?
|
Top
|
|
|
|
#145618 - 2005-08-16 04:31 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
Howard, Maciep is right. The broader scope of this script is not completed as of now.
Maciep, I thought about that, but every time I have tried this on previous scripts it makes the script run an EXTEMELY LONG time. Not something I want to do with this script if it can be helped. I seem to get an error that the machine can not be contacted if the machine is off and the script does not abend. Due to this I dont think I really HAVE to ping the machine.
|
Top
|
|
|
|
#145619 - 2005-08-16 04:34 PM
Re: Script ending prematurely
|
maciep
Korg Regular
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
How did you ping machines in the past - i've never had any problem with wmiPing()
|
Top
|
|
|
|
#145620 - 2005-08-16 04:48 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
It was using the a function from these boards that goes out to %comspec%. I have not tried wmiping.
If I am reading it right, it only requires 2000/xp on the machine running the script, correct?
Thanks,
|
Top
|
|
|
|
#145622 - 2005-08-16 05:06 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
The reason I ask this is the host machine (running script) will be XP, but a number of the client machines (machine being checked) will be Windows NT 4.x. I have deployed WMI and ADSI to all of them though.
Thanks
|
Top
|
|
|
|
#145623 - 2005-08-16 06:28 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
So far, with WMIPing, it appears to have made it farther than on previous tries.
Will inform later if it completes.
Thanks
|
Top
|
|
|
|
#145624 - 2005-08-16 06:40 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
It actually completed with the machines in the ou using WMIPing. That makes me curious, as to which one it skipped in the WMIPing error sections (I did not ask it to tell me about WMIPing errors yet). Time to check that and see what it says.
|
Top
|
|
|
|
#145625 - 2005-08-16 08:12 PM
Re: Script ending prematurely
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
What would the Serror value 'Incorrect function.0' indicate for WMIPing?
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 165 anonymous users online.
|
|
|