Three suggestions that pop up while browsing through your script.

  • You are mapping drives based an group membership. Sure you can just map every time but if the drive is already mapped remapping it will fail. Users can also unmap and map a letter to a different share. This can screw up things that rely on a drive being mapped to a certain share. Deleting the drive before mapping would be better and safer. What I do is check if the drive exists, if it does check the path it maps to and delete and map or leave it alone depending on the results. This way all drives are always mapped to the correct share.
  • The AddPrinterConnection function will return the results of mapping the printer. In your script it show the results on the screen. Might be a bit messy. Doing it like this way $rc = AddPrinterConnection(bla.....) all return codes are stuffed in the $rc variable. It acts like a garbage can collecting all unwanted screen output. Off course you can use any name you like for the variable. I use $rc as it stands for Return Code in my scripting book. For printermapping the same things goes as for drive mapping. Checking it delting and mapping or leave it alone if all is ok would be safer.
  • For the screen resolution you do If 0 = exist(bla....) You could also just do if exist(bla....)


As for error checking I use a setup like below.
First I start a small script that checks and creates new log files.
 Code:
;Set name for text log file.
$txtlog = "c:\logfolder\txtlog.txt"
;Set name for ini log file.
$inilog = "c:\logfolder\inilog.ini"

;Check if and old text log exists.
If Exist( "c:\logfolder\txtlog.txt")
	;Delete old text log.
	Del "c:\logfolder\txtlog.txt"
	;Create new empty text log by opening a file for writing and creating one if none exists.
	$rc = Open(1, "c:\logfolder\txtlog.txt", 5)
	;Close new empty text log.
	$rc = Close(1)
Else
	;Create new empty text log by opening a file for writing and creating one if none exists.	
	$rc = Open(1, "c:\logfolder\txtlog.txt", 5)
	;Close new empty text log.	
	$rc = Close(1)
EndIf

;Check if and old ini log exists.
If Exist( "c:\logfolder\inilog.txt")
	;Delete old ini log.	
	Del "c:\logfolder\inilog.txt"
	;Create new empty ini log by opening a file for writing and creating one if none exists.
	$rc = Open(1, "c:\logfolder\inilog.txt", 5)
	;Close new empty ini log.
	$rc = Close(1)
Else
	;Create new empty ini log by opening a file for writing and creating one if none exists.	
	$rc = Open(1, "c:\logfolder\inilog.txt", 5)
	;Close new empty ini log.
	$rc = Close(1)
EndIf


Then my scripts run and write stuff to the error logs if something goes wrong.
 Code:
;Write entry to log files if an error occurs.

;Read current number of errors from ini log and add 1.
$errornumber = ReadProfileString($inilog, "Errors", "NumberOfErrors") + 1
;Write new number of errors to ini based log file.
$rc = WriteProfileString($inilog, "Errors", "NumberOfErrors", $errornumber)
;Write entry in the text based log file.
$rc = WriteLog($txtlog, @WKSTA + ". User ID: " + @USERID + ". IP address: " + @IPADDRESS0 + ". - ERROR: ERROR TEXT GOES HERE.", 1)


When all scripts are done I run a small script that checks for errors and notifies IT by e-mail of there are more the 0 errors. It adds the text log file to the e-mail.
 Code:
;All scripts are done. Check for errors and notify IT if there are more then 0 errors.
$errornumber = ReadProfileString($inilog, "Errors", "NumberOfErrors")
If $errornumber <> "" And $errornumber <> "0"
	;Set a sender address
	$Sender = "name@@domian.com"
	;Set a recipient address.
	$Recipients = "name@@domain.com"
	;Set the subject.
	$Subject = '"Report - Logon script error on: ' + @WKSTA + '"'
	;Set an attachment.
	$Attachment = '"' + $txtlog + '"'
	;SMTP server to be used for sending the message.
	$smtpserver = "mailserver"
	;Create the body text.
	$Body = '"Logon script error on: ' + @WKSTA + '.' + @CRLF + @CRLF +  
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + @CRLF + 
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' ERROR - Number of logon errors that occured during this session: ' + $errornumber + '.' + @CRLF +
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' User ID: ' + @USERID + ' - ' + @FULLNAME + '.' + @CRLF +
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' System ID: ' + @WKSTA + '.' + @CRLF +
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' IP address: ' + @IPADDRESS0 + '.' + @CRLF +
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' OS name: ' + @PRODUCTTYPE + '.' + @CRLF +
	$mdayno + '-' + $monthno + '-' + @YEAR + ' @@ ' + @TIME + ' OS servicepack level: ' + @CSD + '.' + @CRLF + '"'
	;Send the message.
	$Send = BlatMailer($Recipients, $Sender, $Subject, $Body, $Attachment, $smtpserver)
Else
	;No errors occurred. Do nothing.
EndIf


This requires two UDF's and Blat.exe. Blat can be downloaded for free from many sites. If you Google on Blat.exe there will be lots of links to download it.

Writelog UDF:
 Code:
;FUNCTION WriteLog()
;
;AUTHOR
;		Howard A. Bullock (hbullock@tycoelectronics.com)
;
;ACTION
;		Generic logging facility for scripts. Appends log entry to a file with an optional TimeStamp.
;
;SYNTAX
;		WriteLog($File, $text, [0|1])
;
;PARAMETERS
;		$file (Required) - String value
;		$text (Required) - String value
;		$TimeStamp (Optional) Default(0) no TimeStamp (1 or 0)
;
;REMARKS
;		This function writes (appends) an optionally time stamped log entry to the file defined in function.
;		This function searches for the first unused file handle, open the file, and write the entry. T he file handle
;		is then closed. When the function is unable to perform its it write the error is displayed&nbs p;in a message box.
;
;RETURNS
;		Nothing
;
;DEPENDENCIES
;		None
;
;EXAMPLES
;		WriteLog("junk.txt","This is a test")
;		WriteLog("junk.txt","This is a test",0)
;		WriteLog("junk.txt","This is a test",1)
;
Function WriteLog($File, $Text, optional $TimeStamp)
	Dim $rc, $fh
	$fh = 1
	$rc = Open($fh, $file, 5)
	While $rc = -3
		$fh = $fh + 1
		$RC = Open($fh, $file, 5)
	Loop
	Select
		Case $rc = 0
			If ($timestamp = 1)
				$timestamp = @Date + " @@ " + @Time + " - "
			Else
				$timestamp = ""
			EndIf
			$rc = WriteLine($fh, $timestamp + $text + @CRLF)
			$rc = Close($fh)
		Case $rc = -2
			$text = "WriteLog: Invalid file handle " + $fh + " specified when trying to open " $file + "."
			$rc = MessageBox($text, "Script Error", 48)
		Case $rc = -1
			$text = "WriteLog: Invalid file name " + $file + " specified for log file."
			$rc = MessageBox($text, "Script Error", 48)
		Case $rc = > 0
			$text = "System Error " + $rc + " while attempting to open log file " + $File + "."
			$rc = MessageBox($text, "Script Error", 48)
	EndSelect
EndFunction


and the BlatM ailer UDF.
 Code:
Function BlatMailer($Recipient,$Sender,$Subject,$Body,$Attachment,$smtpserver)
	Dim $MailerLine
		$MailerLine= 'path to blat.exe\blat.exe -' + ' -to ' + $recipient + ' -f ' + $sender + ' -subject ' + $subject + ' -body ' + $body + ' -Attach ' + $Attachment + ' -server ' + $smtpserver + ' -q '
	Shell $MailerLine
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.