I'm starting an exe application through a .bat file, I want to start it granting admin privileges to it. How can I do it in a .bat file.
Two basic options:
Run the script as admin
Use the 'runas' command e.g.
runas /user:administrator blah.exe
Runas will then prompt for the admin password.
Related
I was wondering if there was any code that would make the current script run in Admin privileges. So I can run a batch file and it is something like
run as admin*
{code}
It is an automaticly run script so I can't right click and run as admin, and I want to be clean and not need another script to use runas to run it as an admin.
I've tried runas, and pointing it to that script it is in, with no luck.
I want to run a batch script as Administrator which, in turn, starts another batch script (start "title" /b /wait), but with current user's privilege.
Is this possible? What's the best way to do it?
Or how can I get the current logged in user name?
The command you want is runas - this command runs batchfile with the domain and username of the currently logged in user (you could also hardcode the domain and username if you wanted a specific user):
runas /noprofile /user:%USERDOMAIN%\%USERNAME% C:\batchfile.bat
The runas command requires an external program as the last argument so it doesn't handle built in commands like dir or copy. But you can run a built in command like copy with:
runas /noprofile /user:%USERDOMAIN%\%USERNAME% "cmd /c copy C:\source.txt C:\dest.txt"
You can also run things as Administrator like this:
runas /noprofile /user:Administrator "cmd /c copy C:\file.txt C:\Windows\System"
Here is some more info:
https://www.windows-commandline.com/windows-runas-command-prompt/
I have question how run file .bat as admin? How run cmd as admin in file bat?
I must run command on cmd:
%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -px "MyKey" c:\key.xml -pri
Please help and have any tips or any good material.
Use RUNAS command for this purpose like below. See help using runas /? in command prompt
runas /user:administrator "%windir%\Microsoft.NET\Framework\v2.0.5
0727\aspnet_regiis.exe -px "MyKey" d:\key.xml -pri"
Enter the password for administrator:
open start menu by pressing windows key type cmd right click cmd and select Run as administrator this will open the command prompt in administrator mode and then you can run your command as an administrator.
I have a powershell script. To run this sript, I have written a batch file.
Here is code of batch file:
:: psscript.bat
set psscript='%CD%\Hotfix-Automation-Installer.ps1'
echo Running PowerShell Script: %psscript%
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&%psscript% %*
When I double-click on batch file, I want my script Hotfix-Automation-Installer.ps1 to be run as an administrator.
How I can run this script as administrator?
Read Matt's article on this link:
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
He has given complete script to elevate from current session to administrator session. Quiet helpful.
I have a batch file that starts with elevated privileges (my installer spawns it), but at a certain point I need to run a command as the original user who started my installer (i.e. drop from the elevated privileges).
Is it possible to do so?
You can run a command with restricted privileges with:
runas /trustlevel:0x20000 "YourCommandHere"
You should provide the absolute path to your command including any arguments in double quotes as an argument to runas.
If you would like to run more than one command with restricted privileges, you can put them in a separate batch file and run it with:
runas /trustlevel:0x20000 "cmd /C PathToYourBatchFile"
Anyway, this will open a new console with restricted privileges. You also have to use this syntax whenever you wish to run with restricted privileges an internal command (like copy, del, etc.) as these are provided by the command line interpreter and do not have an associated path.
Note that 0x20000 is the trust level of standard users. You can list other available trust levels by running
runas /showtrustlevels
It's still a privileged program (though restricted) in Task Manager by using this command:
runas /trustlevel:0x20000 <cmd>
You can try the other way, which will make it unprivileged in Task Manager:
runas /savecred /user:%username% <cmd>
You still need to enter the password once but not every time.
Use explorer.exe to launch the program:
explorer.exe <cmd>
explorer.exe won't accept arguments for cmd, but you can create a temp script file and lauch it by explorer.exe if arguments are necessary.