Radimus
|
(KiX Supporter)
|
2006-01-06 07:50 PM
|
|
|
|
|
ReplacePrinter() UDF RFC
|
|
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
|
Lonkero
|
(KiX Master Guru)
|
2006-01-06 08:04 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
looks complex. what's wrong with: Code:
function ReplacePrinter($oldprinter, $newprinter) $=addprinterconnection($newprinter) if $oldprinter = join(split(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'') $=setdefaultprinter($newprinter) endif $=delprinterconnection($oldprinter) Endfunction
|
Radimus
|
(KiX Supporter)
|
2006-01-06 08:11 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
must only add printer2 if printer1 is connected
|
NTDOC
|
(KiX Master)
|
2006-01-06 08:30 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
Just curious why does it have to rely on WMI in the first place? Looks like you're just reading what's in the registry anyways. Pure KiX would be faster wouldn't it?
|
Lonkero
|
(KiX Master Guru)
|
2006-01-06 08:34 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
ja, rip the needed check code from primapstate()
|
Radimus
|
(KiX Supporter)
|
2006-01-06 09:01 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
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
|
Lonkero
|
(KiX Master Guru)
|
2006-01-06 09:20 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
there is no such thing as Default. at least it returns empty vartype always for me.
|
Radimus
|
(KiX Supporter)
|
2006-01-06 09:21 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
if works for me... 0 if not default printer, -1 it default
|
Lonkero
|
(KiX Master Guru)
|
2006-01-06 09:39 PM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
k, this thingie is XP/2003 only property.
the alternative for non XP wksta is: if $collItem.attributes & 4 "default" endif
|
NTDOC
|
(KiX Master)
|
2006-01-07 12:00 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
Nothing wrong with using WMI just that it is a bit more limiting than pure KiX
|
Radimus
|
(KiX Supporter)
|
2006-01-07 12:55 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
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
|
NTDOC
|
(KiX Master)
|
2006-01-07 01:42 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
Thanks for changing the var from $ to $_
I dislike the sinlge $ when used in production code that is not related to Golf, makes it a pain to find or replace the var in bigger code scripts.
|
Lonkero
|
(KiX Master Guru)
|
2006-01-07 01:50 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
lol. why would you want to find and replace a var from inside a UDF? I mean, you shouldn't. check out the how to use udf's FAQ. I bet it says: "don't change the udf source"
|
NTDOC
|
(KiX Master)
|
2006-01-07 01:57 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
ROFLMAO - sure, but this is Script not UDF yet.
But regardless - just don't like single $ myself.
|
Kdyer
|
(KiX Supporter)
|
2006-01-07 02:04 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
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
|
Lonkero
|
(KiX Master Guru)
|
2006-01-07 02:18 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
sorry doc to disappoint you but Function-EndFunction keyword pair does denote UDF. it does not need to stand in the UDF library to be counted as UDF.
|
NTDOC
|
(KiX Master)
|
2006-01-07 02:22 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
True true... but regardless. Still does not change the fact that I don't like single character $ vars.
Oh!... and I often reformat UDFs that I use to conform to my own personal preference anyways. So just wanted to thank Rad for making the change.
Thanks again Rad - you is RAD!!
|
Radimus
|
(KiX Supporter)
|
2006-01-07 02:53 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
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
|
Lonkero
|
(KiX Master Guru)
|
2006-01-07 03:03 AM
|
|
|
|
|
Re: ReplacePrinter() UDF RFC
|
|
well, I think we suggested similar before and rad clearly said that it's not good because he wants to do it with WMI as he loves WMI.
|