Sign in to follow this  
Followers 0
gshaw

RunAs for VBS... think I'm going mad!

11 posts in this topic

Been trying to do this for ages now and it's driving me nuts!

Basically we've got an old 16-bit app that can only print to a local IP printer... which needs admin rights to map. I've got a VBS script I found to do it and want to use AutoIt to run the VBS with elevated rights and then compile to an EXE to hide the credentials.

Found this...

RunAs("USERNAME", "DOMAIN", "PASSWORD", 0, 'wscript.exe "' & @ScriptDir & '\print.vbs"')

But when compiled and run I get wscript access denied error?! The wscript process is running as the admin account I put in the gaps above so what's going on?

Share this post


Link to post
Share on other sites



What is the WorkDir at the time you run this?

Try setting it to a directory that you are sure of is accessible for the account used in the RunAs() command.


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

The directory should definitely be accessible, tried it with a high level account that should be able to access all areas!

The script fails at line 25, where it starts to try and add the port so it seems somehow the script isn't getting the admin privileges even though the wscript is?

Share this post


Link to post
Share on other sites

The directory should definitely be accessible, tried it with a high level account that should be able to access all areas!

The script fails at line 25, where it starts to try and add the port so it seems somehow the script isn't getting the admin privileges even though the wscript is?

Does this make a difference?

RunAs("USERNAME", "DOMAIN", "PASSWORD", 0, @comspec & ' /c ' & @WindowsDir & '\system32\wscript.exe "' & @ScriptDir & '\print.vbs"')

Share this post


Link to post
Share on other sites

Does this make a difference?

RunAs("USERNAME", "DOMAIN", "PASSWORD", 0, @comspec & ' /c ' & @WindowsDir & '\system32\wscript.exe "' & @ScriptDir & '\print.vbs"')

Just gave it a go but only saw a brief flicker of a CMD window pop up and then disappear?

Share this post


Link to post
Share on other sites

Try to use the /k switch to let the command window 'stay' after execution.

RunAs("USERNAME", "DOMAIN", "PASSWORD", 0, @comspec & ' /k ' & @WindowsDir & 'system32wscript.exe "' & @ScriptDir & 'print.vbs"')

> there are 10 types of people in the world, those who understand binary and those who don't.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Just gave it a go but only saw a brief flicker of a CMD window pop up and then disappear?

Edit: just found why that didn't work... forum removed all the slashes from the path!

Got it working but still received the same error when the script tried to run... access denied on line 25

For reference here's the vbscript it's trying to run...

My guess is that somewhere in the vbscript it's using the credentials of the currently logged-on user rather than the one that called the script i.e. the autoit RunAs user?

PrnName = "StudyScan Printer"
PrnLocation = "Reception"
PrnComment = "Printer for StudyScan software"
PrnDrv = "HP LaserJet 4"
DrvPath = "C:\WINDOWS\system32\spool\drivers\w32x86\3"
InfPath = DrvPath & "\UNIDRV.DLL"
PortIP = "10.10.10.10"
PortName = "IP_" & PortIP

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

''''''''''''''''''''''''''
' create ip-printer-port
''''''''''''''''''''''''''
Set objNewPort = objWMIService.Get _
    ("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = PortName
objNewPort.Protocol = 1
objNewPort.HostAddress = PortIP
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

' wsh.echo "port created"

''''''''''''''''''''''''''
' install printer driver
''''''''''''''''''''''''''
' If the driver is not signed, one cannot use WMI scripting
' to install the driver. 
' Make sure the cat file for the package is copied to the same
' location as the driver 

objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set objDriver = objWMIService.Get("Win32_PrinterDriver")

objDriver.Name = PrnDrv
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.DriverPath = DrvPath
objDriver.Infname = InfPath
intResult = objDriver.AddPrinterDriver(objDriver)

' wsh.echo "driver installed"

''''''''''''''''''''''''''
' Add local printer
''''''''''''''''''''''''''
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

objPrinter.DriverName = PrnDrv
objPrinter.PortName = PortName
objPrinter.DeviceID = PrnName
objPrinter.Location = PrnLocation
objPrinter.Comment = PrnComment
objPrinter.Network = True
objPrinter.Put_
 
' wsh.echo "printer added"
Edited by gshaw

Share this post


Link to post
Share on other sites

The VBS script uses WMI . On which operating system do you run it?

Also if you log in with an admin account and run the script does it work?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

The VBS script uses WMI . On which operating system do you run it?

Also if you log in with an admin account and run the script does it work?

Log onto the computer with the local administrator account, then try to run the script. there is a convertor for vbs to autoit that i have used. I scripted the printmig.exe tool that Microsoft created. Its in the example forum. http://www.autoitscr...=1&#entry698665

I found that while you can use RunAs, its better if you dont have to.

You could use my tool, and create the printer on one computer, then backup the printer configuration using the tool. You can then hard code the tool to restore that file to any pc you run it on. You need to run the tool using an account with local admin rights. We dont allow local accounts to have any network access in our environment, so i have it mapping the network drive where the files sit using an account that only has access to that share.

Edited by dot45

Share this post


Link to post
Share on other sites

Been trying to do this for ages now and it's driving me nuts!

Basically we've got an old 16-bit app that can only print to a local IP printer... which needs admin rights to map. I've got a VBS script I found to do it and want to use AutoIt to run the VBS with elevated rights and then compile to an EXE to hide the credentials.

Found this...

RunAs("USERNAME", "DOMAIN", "PASSWORD", 0, 'wscript.exe "' & @ScriptDir & '\print.vbs"')

But when compiled and run I get wscript access denied error?! The wscript process is running as the admin account I put in the gaps above so what's going on?

Don't you think it will help to try and convert the VBS script to AutoIt Script. Just asking.

I don't use vb scripts anymore. I converted all my scripts to AutoIt.

Share this post


Link to post
Share on other sites

Don't you think it will help to try and convert the VBS script to AutoIt Script. Just asking.

I don't use vb scripts anymore. I converted all my scripts to AutoIt.

Tried to do that but the converter failed so went for other avenues...

In the end I found "rundll printui.dll" from a batch file did the trick... called it from AutoIt using RunAs and it did the job perfectly :)

Also had to code in a VBS popup box to act as a splash screen while the driver installation was going on but eventually it's all doing what it's meant to and solves the problem so result!

Thanks for the advice and info :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0