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
891 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 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now