Page 1 of 1 1
Topic Options
#173503 - 2007-02-01 03:53 PM Wildcards in registry?
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Hi,
We are changing printservers. And lets say the old servers name is "oldserver" and the new "newserver"

Im trying to build a script that adds specific printers by different groups in AD. That is not a problem and it is solved. The problem is to delete all printers from oldserver.

To delete one specific printer i use :

If ingroup("Floor3")

$ReturnCode = KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,oldserver,Hp_laserjet")
If $ReturnCode = 1
? "If Hp_laserjet exists it will be deleted"
If delPrinterConnection ("\\oldserver\Hp_laserjet") = 1
? "Removed printer Hp_laserjet"
Endif
Endif
Endif

But what i want to do is to delete all printers added on "oldserver" Can I in some way use wildcard?

Please help!

Top
#173504 - 2007-02-01 04:02 PM Re: Wildcards in registry? [Re: JohnnyBoy]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
A few weeks ago, someone else asked about the same question...
I think PriMapState() can help you.
Look also here:
http://www.kixtart.org/forums/ubbthreads...true#Post171519


Edited by Witto (2007-02-01 04:11 PM)

Top
#173505 - 2007-02-01 04:09 PM Re: Wildcards in registry? [Re: Witto]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Im still searching but I cant find it =( Please tell me how och a link to the old thread.... Thanks!
Top
#173506 - 2007-02-01 04:12 PM Re: Wildcards in registry? [Re: JohnnyBoy]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Just changed my previous post...
But in the previous post, the idea was different. If a user is connected to a printer on the old server, replace the printer with the one on the new server.


Edited by Witto (2007-02-01 04:20 PM)

Top
#173508 - 2007-02-01 04:28 PM Re: Wildcards in registry? [Re: Witto]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Im a too-much-newbie to kix to get this to work. The exaples are great but a bit too complex for me. The only thing i want is to delete all added printers from "oldserver". Not add new ones or so. Just the code to delete the old ones without having the specified printername.

Help ......

Top
#173510 - 2007-02-01 04:41 PM Re: Wildcards in registry? [Re: JohnnyBoy]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Now im trying something like this, but i wont work. Im I close?

$key = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"

$i = 0

$printer = enumvalue($key, $i)


$server = split($printer,"\")[2]
$prtname = split($printer,"\") [3]

if $server
?"**************************************"
?"server is $server for printer $prtname"
?"**************************************"
endif
;fix the connections
if $server = "oldserver"
DELPRINTERCONNECTION("$printer")
?"removed old printer-connection"


endif

Top
#173511 - 2007-02-01 04:52 PM Re: Wildcards in registry? [Re: JohnnyBoy]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I liked your first script. Just loop through all the keys referring to an added printer on the workstation in the user profile.
Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")

Dim $Ptr, $index
$index = 0

$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
While NOT @ERROR
	"Printer with index " + $index + ": " + $Ptr ?
	$index = $index + 1
	$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
Loop

Top
#173514 - 2007-02-01 05:25 PM Re: Wildcards in registry? [Re: Witto]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Here, you can delete the keys. Be sure you do not want to replace them like in the old thread.
Delete is forever.
Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")

Dim $Ptr, $index, $OldServer
$index = 0
$OldServer = "OldServer"

$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
While NOT @ERROR
	;"Printer with index " + $index + ": " + $Ptr ?
	If InStr($Ptr,$OldServer)
		;"KeyName to delete: " + "HKCU\Printers\Connections\" + $Ptr + "\" ?
		$RC = DelKey("HKCU\Printers\Connections\" + $Ptr + "\")
	EndIf
	
	$index = $index + 1
	$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
Loop

Top
#173520 - 2007-02-01 09:30 PM Re: Wildcards in registry? [Re: Witto]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Thank you very much!! That is exactly what I wanted. Many thanks mighty Witto.
Top
#173522 - 2007-02-01 09:44 PM Re: Wildcards in registry? [Re: JohnnyBoy]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Witto : hmmz .... it only deletes one printer. If i run it again it deletes the second... and so on. But isnīt it already looped in the code you posted?
Top
#173526 - 2007-02-01 10:29 PM Re: Wildcards in registry? [Re: JohnnyBoy]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
hmmz. I added 8 printers from the old server. First run the script deleted 4 printers. Second run i deleted 2 printers. And third run the other 2.

Is that strange!? =)

Top
#173533 - 2007-02-02 01:03 AM Re: Wildcards in registry? [Re: JohnnyBoy]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It is expected behavior and a noobie mistake. When you enum the keys, the index is cast in stone. As you delete individual keys, you screw up the index.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#173534 - 2007-02-02 01:03 AM Re: Wildcards in registry? [Re: JohnnyBoy]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I see. I think because I first take index 0 and delete that one. Then everything shifts one up...
Maybe enumerate first to get the highest index and then count back to 0.
Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")

Dim $Ptr, $index, $OldServer
$index = 0
$OldServer = "OldServer"

$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
While NOT @ERROR
	;"Printer with index " + $index + ": " + $Ptr ?
	$index = $index + 1
	$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
Loop

If 0 < $index
	$index = $index - 1
	Do
		$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
		If InStr($Ptr,$OldServer)
			;"KeyName to delete: " + "HKCU\Printers\Connections\" + $Ptr + "\" ?
			$RC = DelKey("HKCU\Printers\Connections\" + $Ptr + "\")
		EndIf		
		$index = $index - 1
	Until 0 > $index
EndIf

Top
#173535 - 2007-02-02 01:05 AM Re: Wildcards in registry? [Re: Les]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
You beat me Les
Top
#173582 - 2007-02-03 10:54 AM Re: Wildcards in registry? [Re: Witto]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Now it is really working! Thanks a bunch!! Really glad you offered the time to help me. And I probably be back =)

Edited by JohnnyBoy (2007-02-03 10:55 AM)

Top
#173632 - 2007-02-05 09:56 AM Re: Wildcards in registry? [Re: Witto]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
I see. I think because I first take index 0 and delete that one. Then everything shifts one up...
Maybe enumerate first to get the highest index and then count back to 0.


You could, but it is simpler just to keep deleting index "0" until there are no entries left.

Top
#173634 - 2007-02-05 11:38 AM Re: Wildcards in registry? [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
No, only if the server is "OldServer", else add 1 to $index and evaluate the next key.
But hey, thanks for the hint. \:\)

Top
#173648 - 2007-02-05 04:26 PM Re: Wildcards in registry? [Re: Witto]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Ooops, my bad. I mis-read and thought you were deleting all records.

The alternative method is then not to increment the counter if you are deleting a record:
 Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")
 
Dim $Ptr, $index, $OldServer
$index = 0
$OldServer = "OldServer"
 
$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
While NOT @ERROR
	If InStr($Ptr,$OldServer)
		$RC = DelKey("HKCU\Printers\Connections\" + $Ptr + "\")
	Else
		$index = $index + 1
	EndIf
	$Ptr = EnumKey("HKCU\Printers\Connections\",$index)
Loop

Top
#173649 - 2007-02-05 05:11 PM Re: Wildcards in registry? [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Yep, That is even a shorter solution.
But I think that JohnnyBoy his problem is already solved.

Top
#173765 - 2007-02-08 08:40 AM Re: Wildcards in registry? [Re: Witto]
JohnnyBoy Offline
Fresh Scripter

Registered: 2007-02-01
Posts: 9
Yep! Thanks everyone! My script is already implemented in our system.
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 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.137 seconds in which 0.085 seconds were spent on a total of 13 queries. Zlib compression enabled.

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