Back after a holiday.. However, we are ramping up again as we moving offices..

Anyway, starting to play with some code to do the following:
  • Capture the printers
  • Terminate Citrix Connections
  • Delete the printers
  • Stop the spooler
  • Delete the drivers
  • Start the spooler
  • Add the printers back in

Have the code working with an exception. It deletes the printers and drivers fine. But must be run one more time (see the notes in code).

One more thing, if I run the code, it seems to hose the RightFAX Printer driver as well..

Now, on to the code (yes, I did play with the ARRAYENUMKEY/ARRAYENUMVALUE UDFs too):
Code:

CLS
BREAK ON

;PRINTCAPTURE ;<< If I run the script here from the start, it will add the printers back in
;(1) Kill Citrix Connection Center
?'Killing Citrix Connection, if found'
ENDPROC('WFCRUN32.EXE')
;(2) Capture Printers and set default
?'Capturing Printers'
PRINTCAPTURE(1)
;(3) Delete Printers
?'Deleting Printers'
DelPrinterConnections
;(4) Stop/Start Printer Spooler
?'Stopping Print Spooler'
SERVICESTART('SPOOLER','stop')
;(5) Remove Driver Files
?'Deleting Print Drivers'
DELPRDRIVERS
;(6) Remove Driver Keys
?'Deleting Print Keys'
DELPRKEYS
?'Starting Print Spooler'
SERVICESTART('SPOOLER','start')
;(6) Add Printers and set default
DEBUG on
?'Adding Printers back in'
PRINTCAPTURE
?'Process is complete.. Press a Key'
get $

FUNCTION DELPRKEYS
DIM $reg,$Index,$Key,$rc
;w32x86\0 : Intel Windows NT 3.1 printer drivers
;w32x86\1 : Intel Windows NT 3.5x printer drivers
;w32x86\2 : Intel Windows NT 4.0 printer drivers
;w32x86\3 : Intel Windows 2000 printer drivers
;Win40\0 : Windows 95 printer drivers
$reg='HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
;$retcode=arrayenumkey($reg)
;$retcode=arrayenumkey('HKEY_USERS')

$Index=0
DO
$Key=ENUMKEY($reg,$Index)
IF INSTR($Key,'LaserJet')
;?$reg+'\'+$Key
$rc=DELTREE($reg+'\'+$Key)
ENDIF
$Index=$Index+1
UNTIL Len($Key)=0
ENDFUNCTION


FUNCTION DELPRDRIVERS
DIM $reg,$path,$Index,$Key,$ConfigFile,$DataFile,$HelpFile,$DependentFiles,$fl
;w32x86\0 : Intel Windows NT 3.1 printer drivers
;w32x86\1 : Intel Windows NT 3.5x printer drivers
;w32x86\2 : Intel Windows NT 4.0 printer drivers
;w32x86\3 : Intel Windows 2000 printer drivers
;Win40\0 : Windows 95 printer drivers
$reg='HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$path='%windir%\system32\spool\drivers\w32x86\3'
;$retcode=arrayenumkey($reg)
;$retcode=arrayenumkey('HKEY_USERS')

$Index=0
DO
$Key=ENUMKEY($reg,$Index)
IF INSTR($Key,'LaserJet')
?$Key
;?' '+$Key1
;Delete Individual files
If READVALUE($reg+'\'+$Key,'Configuration File')<>''
$ConfigFile=READVALUE($reg+'\'+$Key,'Configuration File')
;?'Configuration File '+$ConfigFile
IF EXIST($path+'\'+$ConfigFile)
DEL $path+'\'+$ConfigFile
ENDIF
ENDIF
If READVALUE($reg+'\'+$Key,'Data File')<>''
$DataFile=READVALUE($reg+'\'+$Key,'Data File')
;?'Data File '+$DataFile
IF EXIST($path+'\'+$DataFile)
DEL $path+'\'+$DataFile
ENDIF
ENDIF
If READVALUE($reg+'\'+$Key,'Help File')<>''
$HelpFile=READVALUE($reg+'\'+$Key,'Help File')
;?'Help File '+$HelpFile
IF EXIST($path+'\'+$HelpFile)
DEL $path+'\'+$HelpFile
ENDIF
ENDIF
;If READVALUE($reg+'\'+$Key,'Dependent Files')<>''
$DependentFiles=READVALUE($reg+'\'+$Key,'Dependent Files')
FOR EACH $fl IN SPLIT($DependentFiles,'|')
IF $fl<>''
;?'Dependent Files '+$fl
IF EXIST($path+'\'+$fl)
DEL $path+'\'+$fl
ENDIF
ENDIF
NEXT
;ENDIF
ENDIF
$Index=$Index+1
UNTIL Len($Key)=0
ENDFUNCTION

FUNCTION PRINTCAPTURE(OPTIONAL $capture)
DIM $loc,$reg,$dev,$nprt,$sysdrv,$priReg,$x,$i,$rc,$ps
$loc='HKCU\Network\LPT1'
$reg='HKCU\Software\Microsoft\Windows NT\CurrentVersion'
$dev=$reg+'\Devices'
$nprt=Split(ReadValue($reg+'\Windows','Device'),',')[0]
$sysdrv=SubStr('%WINDIR%',1,2)

$loc='HKCU\Network\LPT1'
$dev='HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices'
$nprt=Split(ReadValue('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows','Device'),',')[0]
If Exist('H:')
$ps='H:\Scripts_DO_NOT_DELETE'
Else
$ps=Left('%WINDIR%',2)+'\Scripts'
EndIf
If 0=Exist($ps)
MD $ps
EndIf
Select
Case
Ltrim(Split(@ipaddress0,'.')[0])+'.'+Ltrim(Split(@ipaddress0,'.')[1])='10.50'
$ps=$ps+'\PDX.ini'
Case
Ltrim(Split(@ipaddress0,'.')[0])+'.'+Ltrim(Split(@ipaddress0,'.')[1])='10.52'
$ps=$ps+'\GEMINI.ini'
Case
1
$ps=$ps+'\PRINTSETUP.ini'
EndSelect

IF $CAPTURE=1
$i=0
Do
If '\\'=Left($x,2)
$priReg=$priReg+$x
EndIf
$x=EnumValue($dev,$i)
$i=$i+1
Until @error

If Len($priReg)
$rc=WriteProfileString($ps,'Printers','','')
$priReg=Split($priReg,'\\')
For $i=1 to Ubound($priReg)
$rc=WriteProfileString($ps,'Printers',$i,'\\'+$priReg[$i])
Next
EndIf
ENDIF

Select
Case InStr($nprt,'\\') ;WRITE THE DEFAULT PRINTER TO THE CONFIG
$rc=WriteProfileString($ps,'Printers','Default',$nprt)
Use LPT1: /delete /persistent
Use LPT1: $nprt /persistent
Case Exist($ps) ;NO DEFAULT PRINTER, LOAD PRINTERS FROM SAVED INFO
For Each $key in Split(ReadProfileString($ps,'Printers',''),Chr(10))
$nul=AddPrinterConnection(ReadProfileString($ps,'Printers',$key))
Next
$nprt=ReadProfileString($ps,'Printers','Default')
$rc=SetDefaultPrinter($nprt)
Use LPT1: $nprt /persistent
Case Not InStr($nprt,'\\') ;LOCAL PRINTER DEFAULT, SO WE WANT TO REMOVE NETWORK MAPPING
Use LPT1: /delete /persistent
EndSelect
ENDFUNCTION

;NAME: ENDPROC
;AUTHOR: Kent Dyer (leptonator@hotmail.com)
;CONTRIBUTORS: Conrad Wheeler "Radimus", kholm
;ACTION: Terminates a local or remote process using WMI
;SYNTAX: ENDPROC($PROC,optional $COMPUTER)
;VERSION: 1.2
;PARAMETERS: $PROC
; Required string for the name of process you want to kill
; $COMPUTER
; Optional string containing the name of the system you are interested in,
; you could use @WKSTA
;RETURNS: Returncode (0) and PID
;REMARKS: Reference - Terminate all copies of the& nbsp;Notepad process on host TEST01
; Simplified (hopefully) version of kholm's script
; No warning about program being killed.
;DEPENDENCIES: Kixtart 4.0, WMI
;EXAMPLE(s): $COMPUTER = @WKSTA
; $PROC = "NOTEPAD.EXE"
;
; ENDPROC($PROC,$COMPUTER)
;
; Returns:
; 0
; notepad.exe is 1508
Function ENDPROC($PROC, optional $COMPUTER)
Dim $Process
If $COMPUTER=''
$COMPUTER='.'
EndIf
For Each $Process in GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$COMPUTER+'\root\cimv2')
.ExecQuery('Select * from Win32_Process where Name=' +'"'+$Proc+'"')
$Process.Terminate
?$Process.Name+' is '+$Process.ProcessId
Next
EndFunction

;Function SERVICESTART()
;
;Author Kent Dyer (leptonator@hotmail.com)
;
;Version 1.0.1 - 26 - February 2003
; 1.0.0 25 - February 2003 - New script/post
; 1.0.1 26 - February 2003 - Implementation of suggestion from Rad.
;
;Action
; Starts or Stops and Starts a Service on a Local or Remote Workstation/Server.
;
;Syntax SERVICESTART($service,$ServerName)
;
;Parameters
; $service
; Name of service. Recommended to use the name from Keys at -
; HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
;
; $ServerName
; String representing target System
;
;Remarks
; User would need Power User rights or better to stop/start services
; Inspiration was the ePO (ePolicy Orchestrator) script
; "Script for adding in a new ePolicy Orchestrator server"
; http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=2;t=004209
; Figured there had to be a better way than NET START Servicename and NET STOP Servicename
; Pieces of the code is from -
; http://cwashington.netreach.net/depo/view.asp?Index=602&ScriptType=vbscript
; "cisvc" is the Indexing Service
; Related posts:
; "WMIService() - Control NT services"
; http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000190#000000
; "fADSIServiceRun() - ADSI routine that starts, stops ,pause, and continues a service"
; http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000205#000000
;
;Returns
; nothing
;
;Dependencies
; kix2001,windows NT (with WBEM or WMINT4.EXE from MSDN - untested)/2000/XP
;
;Examples
; SERVICESTART("cisvc")
; SERVICESTART("cisvc","MACHINENAME")
;
;
;Source
Function SERVICESTART($service,$state,OPTIONAL $ServerName)
Dim $objServer,$objService
If Not $servername $servername='.' EndIf
$ServerString = 'WinNT://'+$ServerName+',computer'
$objServer=GetObject($ServerString)
$ServiceName=$service
$objService=$objServer.GetObject('Service',$ServiceName)
If $objService.Status<>'ADS_SERVICE_RUNNING'
; - start the service
;?'Starting Service ' + $ServiceName
$objService.Start
EndIf
If $state='stop'
;?'Stopping Service ' + $ServiceName
$objService.Stop
Sleep 1
EndIf
If $state='start'
;?'Starting Service ' + $ServiceName
$objService.Start
EndIf
EndFunction

;http://scriptlogic.com/kixtart/FunctionLibrary_ViewFunction.aspx?ID=DelPrinterConnections
function DelPrinterConnections()
dim $c,$bk,$conn
$c=0
$bk="HKEY_CURRENT_USER\Printers\Connections"
$conn=enumkey($bk,$counter)
while @error=0
$c=$c+1
$conn=delkey($bk+"\"+$conn)
$conn=enumkey($bk,$counter)
loop
endfunction



Any suggestions would be great!

Thanks,

Kent


Edited by kdyer (2005-08-30 09:42 PM)
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's