I've written a script to automatically add tcp/ip printer ports and drivers
based on the user's subnet. The script works fine as administrator or power
user however it will NOT work properly if the user has standard user rights.
I need some assistance working around this. I realize that I could use WMI
to do impersonation and add these ports and drivers. However, due to the
large size of the script and the fact that changing methodologies to WMI
would add probably 20+ lines of code per printer per function, there are 100+
functions with anywhere from 2-15 printers per branch function, I can't
realistically change. I'm using the prnport.vbs and rundll32 printui.dll
methodoligies to accomplish what I need. I've tried to build runas into the
script even passing credentials automatically and it still doesn't work
right. My inclination is that the rundll32 must run under an explorer
session that has admin or power user rights otherwise it fails with not
enough rights. Below is a sample of the code. Any help you can provide
would be great!


Dim oIP, r
On Error Resume Next
oIP = DefaultGateway
Select Case oIP
Case "10.207.127.1"
Set oShell = WScript.CreateObject("WScript.Shell")
r = oShell.RegRead("HKLM\SOFTWARE\Printers\judesk\Installed")
If r <> 1 Then
AddPrintersjudesk
End If

End Select


Function DefaultGateway()
Dim oDG, oDGs, WMI
Set WMI = GetObject("winmgmts:\\.\root\cimv2")
Set oDGs = WMI.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each oDG in oDGs
If Not IsNull(oDG.DefaultIPGateway) Then
If Not oDG.DefaultIPGateway(0) = "0.0.0.0" Then
DefaultGateway = oDG.DefaultIPGateway(0)
Exit For
End If
End If
Next
End Function

Function AddPrintersJUDesk()
'prndriver1 = """QMS ColorScript Laser 1000"""
'prndriver2 = """Kyocera Mita FS-3700"""
prndriver3 = """IBM InfoPrint 20"""
'prndriver4 = """HP LaserJet 4"""
'prndriver5 = """SHARP AR-M277 PCL5e"""
prndriver6 = """Kyocera FS-5900C (KPDL-2)"""
'prndriver7 = """HP LaserJet 5Si"""
'prndriver8 = """HP LaserJet 4100 PCL 5e"""
oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
IP_172.18.102.10 -h 172.18.102.10 -o raw -n 9100"
oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154001 /f
%windir%\inf\ntprint.inf /r IP_172.18.102.10 /m " & prndriver1
oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
IP_172.18.102.11 -h 172.18.102.11 -o raw -n 9100"
oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154002 /f
%windir%\inf\ntprint.inf /r IP_172.18.102.11 /m " & prndriver1
oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
IP_172.18.102.14 -h 172.18.102.14 -o raw -n 9100"
oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154003 /f
%windir%\inf\ntprint.inf /r IP_172.18.102.14 /m " & prndriver1
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Printers"
strKeyPath2 = "SOFTWARE\Printers\judesk"
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath2
strValueName = "Installed"
dwValue = 1
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeypath2,strValueName,dwValue

End Function

Re: Adding TCP/IP Printer Ports and Drivers using a login script by ItsMillerTime4u

ItsMillerTime4u
Mon Jun 18 16:45:14 CDT 2007

In PrimalScript and AdminScriptEditor it allows you to compile the
script code and execute it as a different user. if you don't have
either one of these IDEs then you may want to look at possibly writing
your code in AutoIt and use the RunAsSet command see here:
http://www.autoitscript.com/autoit3/docs/functions/RunAsSet.htm then
you can compile the code in the free AutoIt2Exe which is included in
the AutoIt v3 Download.


Re: Adding TCP/IP Printer Ports and Drivers using a login script by billy

billy
Tue Jun 19 23:01:17 CDT 2007

On Jun 15, 2:40 pm, Johnathan <Johnat...@discussions.microsoft.com>
wrote:
> I've written a script to automatically add tcp/ip printer ports and drivers
> based on the user's subnet. The script works fine as administrator or power
> user however it will NOT work properly if the user has standard user rights.
> I need some assistance working around this. I realize that I could use WMI
> to do impersonation and add these ports and drivers. However, due to the
> large size of the script and the fact that changing methodologies to WMI
> would add probably 20+ lines of code per printer per function, there are 100+
> functions with anywhere from 2-15 printers per branch function, I can't
> realistically change. I'm using the prnport.vbs and rundll32 printui.dll
> methodoligies to accomplish what I need. I've tried to build runas into the
> script even passing credentials automatically and it still doesn't work
> right. My inclination is that the rundll32 must run under an explorer
> session that has admin or power user rights otherwise it fails with not
> enough rights. Below is a sample of the code. Any help you can provide
> would be great!
>
> Dim oIP, r
> On Error Resume Next
> oIP = DefaultGateway
> Select Case oIP
> Case "10.207.127.1"
> Set oShell = WScript.CreateObject("WScript.Shell")
> r = oShell.RegRead("HKLM\SOFTWARE\Printers\judesk\Installed")
> If r <> 1 Then
> AddPrintersjudesk
> End If
>
> End Select
>
> Function DefaultGateway()
> Dim oDG, oDGs, WMI
> Set WMI = GetObject("winmgmts:\\.\root\cimv2")
> Set oDGs = WMI.ExecQuery _
> ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
> For Each oDG in oDGs
> If Not IsNull(oDG.DefaultIPGateway) Then
> If Not oDG.DefaultIPGateway(0) = "0.0.0.0" Then
> DefaultGateway = oDG.DefaultIPGateway(0)
> Exit For
> End If
> End If
> Next
> End Function
>
> Function AddPrintersJUDesk()
> 'prndriver1 = """QMS ColorScript Laser 1000"""
> 'prndriver2 = """Kyocera Mita FS-3700"""
> prndriver3 = """IBM InfoPrint 20"""
> 'prndriver4 = """HP LaserJet 4"""
> 'prndriver5 = """SHARP AR-M277 PCL5e"""
> prndriver6 = """Kyocera FS-5900C (KPDL-2)"""
> 'prndriver7 = """HP LaserJet 5Si"""
> 'prndriver8 = """HP LaserJet 4100 PCL 5e"""
> oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
> IP_172.18.102.10 -h 172.18.102.10 -o raw -n 9100"
> oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154001 /f
> %windir%\inf\ntprint.inf /r IP_172.18.102.10 /m " & prndriver1
> oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
> IP_172.18.102.11 -h 172.18.102.11 -o raw -n 9100"
> oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154002 /f
> %windir%\inf\ntprint.inf /r IP_172.18.102.11 /m " & prndriver1
> oShell.Run "cmd /c cscript c:\windows\system32\prnport.vbs -a -r
> IP_172.18.102.14 -h 172.18.102.14 -o raw -n 9100"
> oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /if /b P6154003 /f
> %windir%\inf\ntprint.inf /r IP_172.18.102.14 /m " & prndriver1
> const HKEY_LOCAL_MACHINE = &H80000002
> strComputer = "."
> Set StdOut = WScript.StdOut
>
> Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
> strComputer & "\root\default:StdRegProv")
>
> strKeyPath = "SOFTWARE\Printers"
> strKeyPath2 = "SOFTWARE\Printers\judesk"
> oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
> oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath2
> strValueName = "Installed"
> dwValue = 1
> oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeypath2,strValueName,dwValue
>
> End Function

you could run the script as a startup script (as opposed to a logon
script). startup scripts run as "system" so the script would have no
problem writing to HKLM (lowly users can't do this - which is why your
script fails).

or, you could set up a print server (MS recommends this). users can
add printers published on print servers without the security problems.