Batch Script to Run as Administrator

后端 未结 12 2044
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 11:18

I\'m writing a client/server checking program but it needs to run as Administrator.

I want this to run silently on my network and users, and I don\'t want the \"Run

相关标签:
12条回答
  • 2020-12-13 11:45

    its possible using syntax:

    RUNAS [/profile] [/env] [/netonly] /user:user Program
    
    Key :
    /profile Option to load the user's profile (registry)
    /env Use current environment instead of user's.
    /netonly Use the credentials specified only for remote connections.
    /user Username in form USER@DOMAIN or DOMAIN\USER
    (USER@DOMAIN is not compatible with /netonly)
    Program The command to execute
    

    example :

    runas /env /user:domain\Administrator <program.exe/command you want to execute>
    
    0 讨论(0)
  • 2020-12-13 11:45

    Create a shortcut and set the shortcut to always run as administrator.

    How-To Geek forum Make a batch file to run cmd as administrator solution:

    Make a batch file in an editor and nameit.bat then create a shortcut to it. Nameit.bat - shortcut. then right click on Nameit.bat - shortcut ->Properties->Shortcut tab -> Advanced and click Run as administrator. Execute it from the shortcut.

    0 讨论(0)
  • 2020-12-13 11:46

    My solution, if you dont need to have .bat file, is to convert the batch file to .exe file then you will be able to set up run as admin.

    0 讨论(0)
  • 2020-12-13 11:50

    As I have not found any simple script so far, here's my two cents:

    set ELEVATE_APP=Full command line without parameters for the app to run
    set ELEVATE_PARMS=The actual parameters for the app
    echo Set objShell = CreateObject("Shell.Application") >elevatedapp.vbs
    echo Set objWshShell = WScript.CreateObject("WScript.Shell") >>elevatedapp.vbs
    echo Set objWshProcessEnv = objWshShell.Environment("PROCESS") >>elevatedapp.vbs
    echo objShell.ShellExecute "%ELEVATE_APP%", "%ELEVATE_PARMS%", "", "runas" >>elevatedapp.vbs
    DEL elevatedapp.vbs
    
    0 讨论(0)
  • 2020-12-13 11:51

    Don't waste your time, use this one line command to run command line as administrator:

    echo createobject("shell.application").shellexecute "cmd.exe",,,"runas",1 > runas.vbs & start /wait runas.vbs & del /f runas.vbs
    

    if you want to start any application with administrator privilege you will just write the hole path for this application like this notepad++ in my program files for example :

    echo createobject("shell.application").shellexecute "%programfiles%\Notepad++\notepad++.exe",,,"runas",1 > runas.vbs & start /wait runas.vbs
    
    0 讨论(0)
  • 2020-12-13 11:57

    Following is a work-around:

    1. Create a shortcut of the .bat file
    2. Open the properties of the shortcut. Under the shortcut tab, click on advanced.
    3. Tick "Run as administrator"

    Running the shortcut will execute your batch script as administrator.

    0 讨论(0)
提交回复
热议问题