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