#154660 - 2006-01-06 07:50 PM
ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
replaces connection from printer1 to printer2 preserves default printer
Code:
$oldprinter='\\server\Printer share1' $newprinter='\\server\Printer share2'
ReplacePrinter($oldprinter, $newprinter)
function ReplacePrinter($oldprinter, $newprinter)
for each $Item in split(WMIQuery('Name','Win32_Printer'),'|') if $item = $oldprinter $=addprinterconnection($newprinter) If not @error $GetDefaultPrinter = join(split(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'') if $GetDefaultPrinter = $oldprinter $=setdefaultprinter($newprinter) endif $=delprinterconnection($oldprinter) endif endif next
Endfunction
It currently used WMIQuery, but I'll eventually change it so it doesn't have that dependancy
|
Top
|
|
|
|
#154662 - 2006-01-06 08:11 PM
Re: ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
must only add printer2 if printer1 is connected
|
Top
|
|
|
|
#154665 - 2006-01-06 09:01 PM
Re: ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I like WMI.. :-)
This will narrow down what I need to replace from WMIQuery: Code:
$objWMIService = GetObject("winmgmts:\\" + @wksta + "\root\cimv2") $colItems = $objWMIService.ExecQuery("Select * from Win32_Printer where Network = True",,48) For each $objItem in $colItems "Default: " + $objItem.Default ? "Name: " + $objItem.Name ? ? Next
|
Top
|
|
|
|
#154667 - 2006-01-06 09:21 PM
Re: ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
if works for me... 0 if not default printer, -1 it default
|
Top
|
|
|
|
#154670 - 2006-01-07 12:55 AM
Re: ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
updated...
Code:
function ReplacePrinter($oldprinter, $newprinter, Optional $ForceDefault) DIM $objWMIService, $colItems, $objItem, $_
$objWMIService = GetObject("winmgmts:\\" + @wksta + "\root\cimv2") $colItems = $objWMIService.ExecQuery("Select * from Win32_Printer where Network = True",,48) For each $objItem in $colItems if $objItem.Name = $oldprinter $_=addprinterconnection($newprinter) If @error exit 1 endif ; if $objItem.Default or $ForceDefault if $objItem.attributes & 4 or $ForceDefault $_=setdefaultprinter($newprinter) endif $_=delprinterconnection($oldprinter) endif next Endfunction
|
Top
|
|
|
|
#154673 - 2006-01-07 01:57 AM
Re: ReplacePrinter() UDF RFC
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
ROFLMAO - sure, but this is Script not UDF yet.
But regardless - just don't like single $ myself.
|
Top
|
|
|
|
#154677 - 2006-01-07 02:53 AM
Re: ReplacePrinter() UDF RFC
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
Quote:
Rad,
What is wrong with the following? Most often when printers move is because you bring up a print server and you have to move 84 printers from one server to another..
Prevent special printers from being deleted
PriMapState() - Checks for existent printerconnection
Thanks,
Kent
well, it does word to detect a specific printer... I suppose if I called it from another UDF it would give me the same functionality.
All that aside I bet it is probably faster than WMI...
function replaceprinter($old, $new, $force) $PMS = primapstate($old) if $pms $_=addprinterconnection($new) if @error exit 1 endif $_=delprinterconnection($old) if $pms = 2 or $force $_ = setdefaultprinter($new) endif endif endfunction
or something to that effect
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 248 anonymous users online.
|
|
|