Solved

How do I pass only the dynamic part of a TS/RDS redirected printer name to a sub in a script?

Posted on 2011-03-24
5
899 Views
Last Modified: 2012-05-11
Hello,
        The "challenge":I need to rename a redirected printer. I need to change/append the (redirected XX) part of a TS/RDS redirected printer to (XX) or ( XX) or (X) or (X) (the number is sometimes a single digit and sometimes double digits as well as I don't know if the space between redirected and the number(s) needs to be retained).
EXAMPLE:
ORIGINAL: IMC-FDPtr1 hp LaserJet 1300 PCL 6 on indianmtn (redirected 2)
NEW: IMC-FDPtr1 (41)
NOTE* all of the original printer name is changed to a known value except for the redirection portion. The last is what I need help on.
I have a working script for all of it working (RobSampson @ EE ROCKS!!!)  except for how to do the last part about the redirected portion.

Thanks!
0
Comment
Question by:NetManaged
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35212031
Hi, for this one, you could do a query that does
"SELECT Name FROM Win32_Printer WHERE Name LIKE '" & strNewName & "*'"

and you should find the full name from that.

Rob.
0
 

Author Comment

by:NetManaged
ID: 35238070
Rob,   I reply with a brilliant...huh?
I think I gave a bad example.
ORIGINAL: IMC-FDPtr1 hp LaserJet 1300 PCL 6 on indianmtn (redirected 27)
CHANGED TO: IMC-FDPtr1 (27)
Found the default printer: IMC-FDPtr1 hp LaserJet 1300 PCL 6 on indianmtn (redirected 27).
Shortened the name:IMC-FDPtr1.
And retained ONLY the redirected number and the parentheses (27) in the new printer name.
RESULT: IMC-FDPTR1 (27).
Is that clearer?
Here is the script so far to add the dynamic redirected number in parentheses to the shortened printer name.



Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "IMC-FDPtr1", "GOBBLE (632)"
objDictionary.Add "P2055", "clearfork" 

Set objScriptExec = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Default=True", "WQL", wbemFlagReturnImmediately + 

wbemFlagForwardOnly)
For Each objItem In colItems
	For Each strOldName In objDictionary
		If InStr(objItem.Name,strOldName) <> 0 Then SetIMCFDPTR1 objItem.Name, objDictionary(strOldName)
	Next
Next
WScript.Quit

Sub SetIMCFDPTR1(strPrinterName, strNewName)
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn setowner -ownr ""n:" & 

objNetwork.UserDomain & "\" & objNetwork.UserName &";""", 0, True
	objShell.Run "C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn ace -ace ""n:" & 

objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""", 0, True
	
	Set colItems = objWMIService.ExecQuery("SELECT Name,Default FROM Win32_Printer WHERE Name='" & strPrinterName & "'", "WQL", 

wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objPrinter In colItems
		blnDefault = objPrinter.Default
		objPrinter.RenamePrinter(strNewName)
	Next

	If blnDefault = True Then
		Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Name='" & strNewName & "'", "WQL", 

wbemFlagReturnImmediately + wbemFlagForwardOnly)
		For Each objPrinter In colItems 
			objPrinter.SetDefaultPrinter()
		Next
	End If
End Sub

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35238575
OK, I think I understand.  Try this.

If it sees (redirected ##) in the old name, it will strip out the ## part, and add (##) to the new name.

Regards,

Rob.
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "IMC-FDPtr1", "GOBBLE (632)"
objDictionary.Add "P2055", "clearfork" 

Set objScriptExec = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Default=True", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	For Each strOldName In objDictionary
		If InStr(objItem.Name,strOldName) <> 0 Then SetIMCFDPTR1 objItem.Name, objDictionary(strOldName)
	Next
Next
WScript.Quit

Sub SetIMCFDPTR1(strPrinterName, strNewName)
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName &";""", 0, True
	objShell.Run "C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""", 0, True
	
	Set colItems = objWMIService.ExecQuery("SELECT Name,Default FROM Win32_Printer WHERE Name='" & strPrinterName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objPrinter In colItems
		If InStr(LCase(objPrinter.Name), "redirected") > 0 Then
			strNumber = Left(Mid(objPrinter.Name, InStrRev(objPrinter.Name, " ")), Len(Mid(objPrinter.Name, InStrRev(objPrinter.Name, " "))) - 1)
			strNewName = strNewName & " (" & strNumber & ")"
		End If
		blnDefault = objPrinter.Default
		objPrinter.RenamePrinter(strNewName)
	Next

	If blnDefault = True Then
		Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Name='" & strNewName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
		For Each objPrinter In colItems 
			objPrinter.SetDefaultPrinter()
		Next
	End If
End Sub

Open in new window

0
 

Author Comment

by:NetManaged
ID: 35291005
Oh yeah!! You da man!
Now to work out the Task Scheduler issue in getting this to run at any Domain User's logon.

Thank Rob!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35291022
Sure, no problem. Thanks for the grade.

Rob.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question