0

I'm trying to install printers through command line which looks like:

start \\servername\\printername

I have to run cmd.exe as admin or it won't install drivers. This works well when running it directly through command line.

However, I put that command into a .bat file again:

Name: printer.bat with content:

start \\servername\\printername

Then I created a link to that file printer.bat.lnk and in Properties under Advanced ticked the "Run as admin" option.

This doesn't work and gets immediately canceled.

Any tips that can resolve my issue?

Thanks

2
  • Perhaps a question of the current directory when executed?
    – harrymc
    Commented Jun 13, 2022 at 13:53
  • It's in a network drive like O:\
    – user642958
    Commented Jun 13, 2022 at 14:48

1 Answer 1

0

Add this to the top of your batch script to have it automatically run as admin

@ECHO OFF

REM --------------------------Start of getting superuser permissions--------------------------------------------

REM Check If the script has admin rights
openfiles.exe 1>nul 2>&1

REM If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
    ) else ( goto gotAdmin )

REM Now we create temp visual basic script which will run this script again with UAC
:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params=%*
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

REM Run getadmin.vbs which will run YourScript.bat with UAC
    "%temp%\getadmin.vbs"
REM Delete getadmin.vbs and exit
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin

REM --------------------------End of getting superuser permissions--------------------------------------------

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .