Page 1 of 1 1
Topic Options
#154660 - 2006-01-06 07:50 PM ReplacePrinter() UDF RFC
Radimus Moderator Offline
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
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154661 - 2006-01-06 08:04 PM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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

_________________________
!

download KiXnet

Top
#154662 - 2006-01-06 08:11 PM Re: ReplacePrinter() UDF RFC
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
must only add printer2 if printer1 is connected
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154663 - 2006-01-06 08:30 PM Re: ReplacePrinter() UDF RFC
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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?
Top
#154664 - 2006-01-06 08:34 PM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ja, rip the needed check code from primapstate()
_________________________
!

download KiXnet

Top
#154665 - 2006-01-06 09:01 PM Re: ReplacePrinter() UDF RFC
Radimus Moderator Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154666 - 2006-01-06 09:20 PM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
there is no such thing as Default.
at least it returns empty vartype always for me.
_________________________
!

download KiXnet

Top
#154667 - 2006-01-06 09:21 PM Re: ReplacePrinter() UDF RFC
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
if works for me... 0 if not default printer, -1 it default
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154668 - 2006-01-06 09:39 PM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
k, this thingie is XP/2003 only property.

the alternative for non XP wksta is:
if $collItem.attributes & 4
"default"
endif
_________________________
!

download KiXnet

Top
#154669 - 2006-01-07 12:00 AM Re: ReplacePrinter() UDF RFC
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Nothing wrong with using WMI just that it is a bit more limiting than pure KiX
Top
#154670 - 2006-01-07 12:55 AM Re: ReplacePrinter() UDF RFC
Radimus Moderator Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154671 - 2006-01-07 01:42 AM Re: ReplacePrinter() UDF RFC
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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.

Top
#154672 - 2006-01-07 01:50 AM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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"
_________________________
!

download KiXnet

Top
#154673 - 2006-01-07 01:57 AM Re: ReplacePrinter() UDF RFC
NTDOC Administrator Offline
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
#154674 - 2006-01-07 02:04 AM Re: ReplacePrinter() UDF RFC
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
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
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#154675 - 2006-01-07 02:18 AM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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.
_________________________
!

download KiXnet

Top
#154676 - 2006-01-07 02:22 AM Re: ReplacePrinter() UDF RFC
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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!!
 

Top
#154677 - 2006-01-07 02:53 AM Re: ReplacePrinter() UDF RFC
Radimus Moderator Offline
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
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#154678 - 2006-01-07 03:03 AM Re: ReplacePrinter() UDF RFC
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.072 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org