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

x
?
Solved

How do I find and pass dynamic data to other parts of a VBScript?

Posted on 2011-03-15
14
Medium Priority
?
658 Views
Last Modified: 2012-08-14
Hello scripters!

        I am attempting to do the following via VBScript in a TS/RDS Farm:
At logon, find the currently logged on user.
Find the current default printer for that Remote Desktop session.
Pass those variables to other lines later in the VBScript to use a SetACL process.
SetACL line(s):
Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on ""<Discovered TS/RDS session default printer>"" -ot prn -actn ace -ace ""n:<domainnamehere>\<discoveredusernamehere>;p:man_printer""")

Thanks!
0
Comment
Question by:NetManaged
  • 8
  • 6
14 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35143358
Hi, this should work when run as the currently logged on user.

Regards,

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

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
	strPrinter = objItem.Name
Next

Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinter & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""")

Open in new window

0
 

Author Comment

by:NetManaged
ID: 35151722
Oh my Rob...that looks like what I need!
I never noticed you could use the objNetwork.<something> like that.
What does the "WQL" signify? I'm not familiar with that.
I hope to be able to test this tomorrow, both, as a standalone and to incorporate into the script I am developing.
Thanks!
Greg
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35152747
WQL stands for Windows Query Language.  It's the language of the queries used for WMI.  It must be specified when the parameters at the end are used.

http://msdn.microsoft.com/en-us/library/aa393866(v=vs.85).aspx

Regards,

Rob.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:NetManaged
ID: 35169138
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

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
      strPrinter = objItem.Name

For Each objPrinter in colItems
if inStr(objPrinter.Name,"IMC-FDPtr1") <> 0 Then Call SetIMCFDPTR1
Sub SetIMCFDPTR1
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinter & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & 

objNetwork.UserName &";""")
strPrnConfig = objScriptExec.StdOut.ReadAll
'WScript.Echo strPrnConfig
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinter & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & 

";p:man_printer""")
strPrnConfig = objScriptExec.StdOut.ReadAll
'WScript.Echo strPrnConfig
     objPrinter.RenamePrinter("IMCFDPTR1 (2)")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'IMCFDPTR1 (2)'")
For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
end sub
0
 

Author Comment

by:NetManaged
ID: 35169149
Ahh nuts! Messed up!
The code you supplied works great! I added a line above it to take ownership first then add the manage printers permission.
How do I pass the strings to a Sub later in the script?
I want to pass the printer in the sub.
When I get to Sub SetIMCFDPTR1 I get a general syntax error.
When I run my original script with known values it works.
What am I doing wrong?
Thanks!
Greg
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35169928
Hi, I don't see your sub in that code, but to pass the string, call
SetIMCFDPTR1 strPrinter

with
Sub SetIMCFDPTR1(strPrinterName)
   WScript.Echo "Printer: " & strPrinterName
End Sub


Regards,

Rob.
0
 

Author Comment

by:NetManaged
ID: 35183103
Guess, I'm still not doing something right.
I still get a syntax error at the Sub.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

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
	strPrinter = objItem.Name

For Each objPrinter in colItems
  if inStr(objPrinter.Name,"IMC-FDPtr1") <> 0 Then Call SetIMCFDPTR1("strPrinter")

wscript.quit

Sub SetIMCFDPTR1(strPrinterName)
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinter & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & 

objNetwork.UserName &";""")
strPrnConfig = objScriptExec.StdOut.ReadAll

Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinter & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & 

";p:man_printer""")
strPrnConfig = objScriptExec.StdOut.ReadAll

     objPrinter.RenamePrinter("IMCFDPTR1 (2)")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'IMCFDPTR1 (2)'")
For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
end sub

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35194575
Hi, very sorry for my delay.  It turns out I was being blind, as your Sub was in the original code. I was also being blind because you have one too many For loops, and no Next statements.  This should work now.

Regards,

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

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
	If inStr(objItem.Name,"IMC-FDPtr1") <> 0 Then SetIMCFDPTR1 objItem.Name
Next
WScript.Quit

Sub SetIMCFDPTR1(strPrinterName)
	Set objShell = CreateObject("WScript.Shell")
	Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName &";""")
	strPrnConfig = objScriptExec.StdOut.ReadAll

	Set objShell = CreateObject("WScript.Shell")
	Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""")
	strPrnConfig = objScriptExec.StdOut.ReadAll

	objPrinter.RenamePrinter("IMCFDPTR1 (2)")
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer Where Name = 'IMCFDPTR1 (2)'")
	For Each objPrinter In colInstalledPrinters
		objPrinter.SetDefaultPrinter()
	Next
End Sub

Open in new window

0
 

Author Comment

by:NetManaged
ID: 35206435
Hm...getting an error at Line 22, Object required: 'objPrinter'
0
 

Author Comment

by:NetManaged
ID: 35208859
Wups! Never mind. I figured out what was wrong.
In the code window is the working script. : )
Please look it over for any optimization/clean up you recommend.
If it is good as-is, I will go ahead and award you the points.
ALSO, I have one more aspect to this script I need help on, but I will open another question for it.

Thanks!
Greg
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

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
	If inStr(objItem.Name,"IMC-FDPtr1") <> 0 Then SetIMCFDPTR1 objItem.Name
Next
WScript.Quit

Sub SetIMCFDPTR1(strPrinterName)
	Set objShell = CreateObject("WScript.Shell")
	Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName &";""")
	strPrnConfig = objScriptExec.StdOut.ReadAll

	Set objShell = CreateObject("WScript.Shell")
	Set objScriptExec = objShell.Exec("C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""")
	strPrnConfig = objScriptExec.StdOut.ReadAll

	Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Default=True", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objPrinter In colItems
	objPrinter.RenamePrinter("IMCFDPTR1 (2)")

	Next

	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer Where Name = 'IMCFDPTR1 (2)'")
	For Each objPrinter In colInstalledPrinters
		objPrinter.SetDefaultPrinter()
	Next
End Sub

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35212008
Hi, OK, a few things that I've rejigged a bit.

1) You're not needing to use objShell.Exec because you don't need to do anything with the output, so we can use objShell.Run and make it run hidden instead.
2) Your original query on line 7 only queried the default printer.  If the printer exists but is not the default, then it won't be actioned, but maybe that was by design.  In any case, I have made it locate any printer with the specified string in it's name, and then in the Sub, you pass it the old name, and the new name, and the Sub will determine whether it was the default or not, and only set the new one default if it was.
3) To identify the printer's old and new names, I've added a dictionary object, so you can easily add and remove extra printers as required

I think that's it.

Try it out and see how it goes.

Regards,

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

Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "IMC-FDPtr1", "IMCFDPTR1 (2)"

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 colInstalledPrinters
			objPrinter.SetDefaultPrinter()
		Next
	End If
End Sub

Open in new window

0
 

Assisted Solution

by:NetManaged
NetManaged earned 0 total points
ID: 35237997
RobSampson,

           After a minor adjustment on line 31 to change colInstalledPrinters to coltems, it worked great!
Added additional lines for objDictionary.Add with old and new printer names as needed!
Attached is the modified code with an additional dictionary entry for displaying how to add more printers (I was erroneously adding them on the same line).

WOOT! Thanks RobSampson!

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

Expert Comment

by:RobSampson
ID: 35238087
Great. No problem. Thanks for the grade.  I'm glad you were able to make sense of my modifications.

Regards,

Rob.
0
 

Author Closing Comment

by:NetManaged
ID: 35304439
RobSampson really helped me!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

On a regular basis I get questions about slow RDP performance, RDP connection problems, strange errors and even BSOD, remote computers freezing or restarting after initiation of a remote session. In a lot of this cases the quick solutions made b…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

773 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