Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
911 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
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

824 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