#146812 - 2005-08-30 09:42 PM
Printer remove/refresh code
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
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)
|
Top
|
|
|
|
#146813 - 2005-09-24 05:40 AM
Re: Printer remove/refresh code
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
OK.. Did some heavy Googling today and found the following batch script code: Code:
@Echo Off :: Found in the c++ Forum of http://msfn.org :: http://www.msfn.org/board/index.php?showtopic=44848&st=0 :: If you run RightFAX or another program like it, you will need to install :: an appropriate driver and re-configure it :: You will need Administrative Rights to run this If Not %1'==/?' Goto Begin Echo Removes Windows NT's printers (Registry-settings and printer driver's files). Echo. Echo %~n0 (Goto :EOF) :Begin (Echo [Version] Echo Signature = "$Windows NT$" Echo. Echo [DefaultInstall] Echo DelReg = DelReg Echo AddReg = AddReg Echo. Echo [DelReg] Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Printers" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-2" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3" Echo HKCU,"Printers\Connections" Echo HKCU,"Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" Echo HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\LanMan Print Services\Servers" Echo HKLM,"SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports" Echo. Echo [AddReg] Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Printers","DefaultSpoolDirectory",,, Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Printers","DefaultSpoolDirectory",,"%%SystemRoot%%\System32\spool\PRINTERS" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-2",,, Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-2","Directory",,"2" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-2","MajorVersion",%%REG_DWORD%%,2 Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-2","MinorVersion",%%REG_DWORD%%,0 Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3",,, Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3","Directory",,"2" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3","MajorVersion",%%REG_DWORD%%,2 Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3","MinorVersion",%%REG_DWORD%%,0 Echo HKCU,"Printers\Connections" Echo HKCU,"Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" Echo HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\LanMan Print Services\Servers" Echo HKLM,"SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports" Echo HKLM,"SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports" Echo. Echo [Strings] Echo REG_SZ = 0x00000000 Echo REG_MULTI_SZ = 0x00010000 Echo REG_EXPAND_SZ = 0x00020000 Echo REG_BINARY = 0x00000001 Echo REG_DWORD = 0x00010001)>%TEMP%.\$%~n0.inf If Not Exist %TEMP%.\$%~n0.inf ( Echo Can not write to: %TEMP%.\$%~n0.inf Goto :EOF) Echo Remove all printers on %COMPUTERNAME% %SystemRoot%\system32\net.exe STOP SPOOLER %SystemRoot%\system32\rundll32.exe setupapi,InstallHinfSection DefaultInstall 128 %TEMP%.\$%~n0.inf Del %TEMP%.\$%~n0.inf For %%F In (%SystemRoot%\system32\spool\drivers\w32x86) Do ( %SystemRoot%\system32\attrib.exe -r -s -h %%F\*.* /S Del /Q /S %%F\*.*) %SystemRoot%\system32\net.exe START SPOOLER
While there may be other methods, this does clean out all printers and drivers.
Kent
Edited by kdyer (2005-09-24 05:42 AM)
|
Top
|
|
|
|
#146814 - 2005-09-24 06:17 AM
Re: Printer remove/refresh code
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Had some time and tried to convert this over to KiX.. Need to work on some details. Author's note: this has not been fully tested on systems with lpr ports, Adobe Acrobat, GhostScript, etc.
Code:
CLS BREAK ON ; -- Step 1, Kill Citrix Connection Center ?'Killing Citrix Connection, if found' ENDPROC('WFCRUN32.EXE') ; -- Would like to change this to where you log people out of Citrix ; -- Step 2, capture the network printers PRINTCAPTURE(1) ; -- Step 3, do the work of removing all print drivers ?"Removes Windows printers (Registry-settings and printer driver's files)." ?'Remove all printers on '+@WKSTA ?'If you have RightFax Installed, you will need to re-configure the print driver' ? SLEEP 2 $env='HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers' $fldr='%SystemRoot%\system32\spool\drivers\w32x86' $print='HKLM\SYSTEM\CurrentControlSet\Control\Print' $print1='HKLM\SYSTEM\CurrentControlSet001\Control\Print'
$cv='SOFTWARE\Microsoft\Windows NT\CurrentVersion' SHELL '%COMSPEC% /C NET STOP SPOOLER' $rc=DELTREE('HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers') $rc=DELTREE('HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers') $rc=DELTREE($env+'\Version-2') $rc=DELTREE($env+'\Version-3') $rc=DELTREE('HKCU\Printers\Connections') $rc=DELTREE('HKCU\'+$cv+'\PrinterPorts') $rc=DELTREE('HKLM\'+$cv+'\Print\Providers\LanMan Print Services\Servers') $rc=DELTREE($print1+'\Monitors\Standard TCP/IP Port\Ports') $rc=DELTREE($print+'\Monitors\Standard TCP/IP Port\Ports')
$rc=WRITEVALUE($print+'\Printers','DefaultSpoolDirectory','%SystemRoot%\System32\spool\PRINTERS','REG_SZ') $rc=WRITEVALUE($env+'\Version-2','Directory',2,'REG_SZ') $rc=WRITEVALUE($env+'\Version-2','MajorVersion',2,'REG_DWORD') $rc=WRITEVALUE($env+'\Version-2','MinorVersion',0,'REG_DWORD')
$rc=WRITEVALUE($env+'\Version-3','Directory',2,'REG_SZ') $rc=WRITEVALUE($env+'\Version-3','MajorVersion',2,'REG_DWORD') $rc=WRITEVALUE($env+'\Version-3','MinorVersion',0,'REG_DWORD')
$rc=ADDKEY('HKCU\Printers\Connections') $rc=ADDKEY('HKCU\'+$cv+'\PrinterPorts') $rc=ADDKEY('HKLM\'+$cv+'\Print\Providers\LanMan Print Services\Servers') $rc=ADDKEY('HKLM\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports') $rc=ADDKEY('HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports')
$Dir = dirplus($fldr,"/s") For Each $file In $dir ? $file $Rc = SetFileAttr($file,128) SHELL '%COMSPEC% /D Del '+$file Next SHELL '%COMSPEC% /C NET START SPOOLER' ; -- Step 4, Add the network printers back in PRINTCAPTURE
?'Process is complete' sleep 2
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=$reg+'\Devices' $nprt=Split(ReadValue($reg+'\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 DIRPlus() ; ;Author Bryce Lindsay bryce@isorg.net ; ;Action Returns an array containing directory files and folders ; ;Syntax DIRPLUS("PATH","OPTIONS") ; ;Version 2.0 ; ;Date Posted Sep-11-2004 ; ;Date Revised Dec-06-2004 ; ;Revision reason Updated the required KiXtart version to 4.22 from 4.02 and some minor typos. ; ;Parameters Path ; Full path to a folder that you want to return information on. ; "c:\program files" ; ; OPTIONS ; /S Displays files In specified directory and all subdirectories. ; ; /A Displays files with specified attributes. ; attributes D Directories R Read-only files ; H Hidden files A Files ready for archiving ; S System files - Prefix meaning not ; ; /M Apply mask string to filter based on InSTR(), separate each search string with an |. ; ; ;Remarks Finally fixed this UDF to handle multiple recursions, ; also should have a faster response time since it is using the FSO ; ; ***Please note that the syntax for version 2.0 of this UDF has changed.*** ; ; made some tweaks using feedback from Les! thanks! ; ;Returns Returns an array of FSO objects that are equal to the file and folder objects of ; the given path. Also returns an @ERROR code for event handling. ; ;Dependencies FSO ; ;KiXtart Ver 4.22 ; ;Example(s) $Dir = dirplus("c:\program files") ;returns all files and folders in the "c:\program files" folder ; $Dir = dirplus("c:\program files","/s") ;all files and folders including subfolders ; $Dir = dirplus("c:\","/a-d") ;returns only a list of files In the c:\ ; $Dir = dirplus("c:\","/ad") ;returns only a list of folders In the c:\ ; $Dir = dirplus("c:\program files","/ad /s") ;returns only the folders including all subfolders. ; ; $Dir = dirplus("g:\kix\udf","/s /ad /m dir") ; recursive subfolder search, folders only, using a mask string of "dir" ; ; $desktop = dirplus("%userprofile%\desktop","/a-d") ; For Each $file In $desktop ; ? $file ; ? $file.size ; Next ; ;Source
Function DirPlus($path,optional $Options) DIM $f, $file, $i, $temp, $item,$sfflag, $ex1, $mask,$maskArray, $ex2, $code, $CodeWeight, $targetWeight, $weight, $masktrue DIM $tarray[0]
$ex1 = SetOption(Explicit,on) $ex2 = SetOption(NoVarsInStrings,on) $codeWeight = 0
If Not Exist($path) Exit(2) EndIf $f = CreateObject("Scripting.FileSystemObject").getfolder($path) If @ERROR $temp = SetOption(Explicit,$ex1) $temp = SetOption(NoVarsInStrings,$ex2) Exit @ERROR EndIf
For Each $temp In Split($options,"/") $temp=Trim($temp) Select Case $temp = "s" $sfflag = 1 Case Left($temp,1) = "a" Select Case Right($temp,-1)="d" $codeWeight = $codeWeight + 1 $temp = "if $file.type = 'File Folder' " Case Right($temp,-1)="-d" $codeWeight = $codeWeight + 1 $temp = "if $file.type <> 'File Folder' " Case Right($temp,-1)="s" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 4 " Case Right($temp,-1)="-s" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 4)=0 " Case Right($temp,-1)="h" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 2 " Case Right($temp,-1)="-h" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 2)=0 " Case Right($temp,-1)="r" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 1 " Case Right($temp,-1)="-r" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 1)=0 " Case Right($temp,-1)="a" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 32 " Case Right($temp,-1)="-a" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 32)=0 " EndSelect $code = $temp + "$weight=$weight+1 EndIf" +@CRLF + $code
Case Left($temp,1) = "m" $maskarray = Split(Right($temp,-2),"|") $codeweight = $codeweight + 1 $code = "$masktrue=0 for Each $mask in $maskarray if instr($file.name,$mask) $masktrue=1 " + "EndIf Next If $masktrue $weight=$weight+1 EndIf" + @CRLF +$code EndSelect Next $code = "$weight = 0 $targetWeight = " + $codeweight + @CRLF + $code $code = $code + "if $weight = $targetweight Exit(1) EndIf"
For Each $file In $f.subfolders If Execute($code) $tarray[$i] = $file $i = $i + 1 ReDIM PreServe $tarray[$i] EndIf If $sfflag $temp = dirplus($file, $options) For Each $item In $temp $tarray[$i] = $item $i = $i + 1 ReDIM PreServe $tarray[$i] Next EndIf Next For Each $file In $f.files If Execute($code) $tarray[$i] = $file $i = $i + 1 ReDIM PreServe $tarray[$i] EndIf Next
If $i ReDIM PreServe $tarray[$i-1] $i=0 Else $tarray = 0 EndIf
$dirplus = $tarray $temp = SetOption(Explicit,$ex1) $temp = SetOption(NoVarsInStrings,$ex2) Exit @ERROR EndFunction
Thanks,
Kent
Edited by kdyer (2005-09-24 06:34 AM)
|
Top
|
|
|
|
#146816 - 2005-09-24 04:41 PM
Re: Printer remove/refresh code
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Quote:
eh, you say that the initial code did not work? doh, finding the batch script does not really help as it brings nothing new.
I am confused.. If you run the batch code, it creates an INF file and runs it. It then takes the attributes off of the files in the spool\drivers directory and deletes them.
So, if we look at this from the objective I am looking for, it covers exactly what I am trying to de.
The only thing that the batch code does not do what I want is to capture the printers before hand and add them back in afterward.
Kent
Edited by kdyer (2005-09-24 04:42 PM)
|
Top
|
|
|
|
#146817 - 2005-10-04 11:03 PM
Re: Printer remove/refresh code
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Was a bit frustrated at work this morning.. So, corraled myself in and re-wrote the script. Thanks Jens for the ARRAYENUMKEY as it works like a champ!
Here goes: Code:
CLS BREAK ON DIM $rc $rc=SetOption('Explicit','On') $rc=SetOption('NoVarsInStrings','On') DIM $admin,$printer,$printers,$pdrivers,$driverdir,$driver,$drivers,$fl,$cfg,$df,$hf,$ps,$key,$nprt,$ $admin=InGroup(@wksta+'\'+SIDtoName('S-1-5-32-544'))-1+@inwin IF $admin=0 'Sorry, you must be an Admin on the machine you are running this on'? 'Exiting the script'? SLEEP 2 ELSE 'Please note: You will need to re-configure RightFAX'? 'To get the printing re-enabled again'? '---'? SLEEP 2 'Ending the Citrix Session - if found'? ENDPROC('WFCRUN32.EXE') 'Capturing the network printers'? PRINTCAPTURE(1) ; -- STOP THE PRINT SPOOLER 'Stopping the Print Spooler'? SHELL '%COMSPEC% /C NET STOP SPOOLER'
;--DELETE LOCAL USER Printers 'Deleting the local user printers'? $printers=arrayenumkey('HKCU\Printers\Connections') FOR EACH $printer IN $printers $printer? $rc=DelTree("HKCU\Printers\Connections\"+$printer) NEXT
; -- Look for Drivers 'Looking for Drivers'? $pdrivers='HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3' $driverdir='%windir%\system32\spool\drivers\w32x86\3\'
$drivers=arrayenumkey($pdrivers) FOR EACH $driver IN $drivers IF NOT InStr($driver,'Adobe') 'Removing Print driver '+$driver? FOR EACH $fl IN Split(ReadValue($pdrivers+'\'+$driver,'Dependent Files'),'|') IF $fl<>'' ; -- CHECK TO INSURE THE FILE EXISTS 'Checking for file: '+$fl? IF Exist($driverdir+$fl) 'Setting Attributes on file: '+$fl? ; -- TURN ANY READ ONLY OFF $rc=SetFileAttr($driverdir+$fl, 128) ; -- DELETE FILE 'Deleting file: '+$fl? DEL $driverdir+$fl ENDIF ENDIF NEXT $cfg=ReadValue($pdrivers+'\'+$driver,'Configuration File') ; -- CHECK TO INSURE THE FILE EXISTS 'Checking for file: '+$cfg? IF Exist($driverdir+$cfg) 'Setting Attributes on file: '+$cfg? $rc=SetFileAttr($driverdir+$cfg, 128) 'Deleting file: '+$cfg? DEL $driverdir+$cfg ENDIF $df=ReadValue($pdrivers+'\'+$driver,'Data File') ; -- CHECK TO INSURE THE FILE EXISTS 'Checking for file: '+$df? IF Exist($driverdir+$df) 'Setting Attributes on file: '+$df? $rc=SetFileAttr($driverdir+$df, 128) 'Deleting file: '+$df? DEL $driverdir+$df ENDIF $hf=ReadValue($pdrivers+'\'+$driver,'Help File') 'Checking for file: '+$hf? ; -- CHECK TO INSURE THE FILE EXISTS IF Exist($driverdir+$hf) 'Setting Attributes file: '+$hf? $rc=SetFileAttr($driverdir+$hf, 128) 'Deleting file: '+$hf? DEL $driverdir+$hf ENDIF 'Deleting Registry Key for driver: '+$driver? $rc=DelTree($pdrivers+'\'+$driver) ENDIF NEXT ; -- START THE PRINT SPOOLER 'Starting the Print Spooler'? SHELL '%COMSPEC% /C NET START SPOOLER'
'Adding the Printers back in'? IF Exist('H:') $ps='H:\Scripts_DO_NOT_DELETE' ELSE $ps=Left('%WINDIR%',2)+'\Scripts' ENDIF IF 0=Exist($ps) MD $ps ENDIF $ps=$ps+'\PRINTSETUP.ini' FOR EACH $key IN Split(ReadProfileString($ps,'Printers',''),Chr(10)) $rc=AddPrinterConnection(ReadProfileString($ps,'Printers',$key)) NEXT $nprt=ReadProfileString($ps,'Printers','Default') $rc=SetDefaultPrinter($nprt) USE LPT1: $nprt /persistent 'Process is complete'? SLEEP 2 ENDIF
FUNCTION PRINTCAPTURE(OPTIONAL $capture) DIM $loc,$reg,$dev,$nprt,$sysdrv,$prireg,$x,$i,$rc,$ps,$key $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=$reg+'\Devices' $nprt=Split(ReadValue($reg+'\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 $ps=$ps+'\PRINTSETUP.ini'
IF $capture $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)) $rc=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 ArrayEnumKey ;ACTION Creates an array of names of the subkeys contained in a registry key or subkey ;AUTHOR Jens Meyer (sealeopard@usa.net) ;VERSION 1.2 (added error codes) ; 1.1 ;DATE CREATED 2001/12/05 ;DATE MODIFIED 2003/05/17 ;KIXTART 4.12+ ;SYNTAX ARRAYENUMKEY($subkey) ;PARAMETERS SUBKEY ; Required string containing the key or subkey for which the subkeys will be enumerated ;RETURNS Array containing the subkeys ;REMARKS none ;DEPENDENCIES none ;EXAMPLE $retcode=arrayenumkey('HKEY_USERS') ;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000064 ; Return an array of subkey names from the primary location FUNCTION ArrayEnumKey($regsubkey) DIM $subkeycounter, $currentsubkey, $subkeyarray IF NOT KeyExist($regsubkey) EXIT 87 ENDIF $subkeycounter = 0 DO $currentsubkey = EnumKey($regsubkey, $subkeycounter) IF NOT @error REDIM PRESERVE $subkeyarray[$subkeycounter] $subkeyarray[$subkeycounter] = $currentsubkey $subkeycounter = $subkeycounter + 1 ENDIF UNTIL @error $arrayenumkey = $subkeyarray EXIT 0 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
Thanks,
Kent
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|