0

This should be a common question and the closest I found is:

runas /noprofile /user:mymachine\administrator yourbatchfile.bat

However the above is not 100% automatic and it will open another cmd window and ask user to type admin password.

My intention is to automatically run a batch file as administrator, and the first step is to get a cmd window with administrator right. A use case is to install 'Chocolatey' when AWS machine boot up.

This seems to be a simple requirement.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1559625
  • 2,583
  • 5
  • 37
  • 75
  • Anyway you could do it via scripting would leave the password exposed. You might be better looking at writing a simple C# or C++ program that created the process using impersonation. At least that way the password wouldn't be in plain text – Code Gorilla Oct 03 '17 at 09:12
  • Launching an elevated PowerShell session (i.e as admin) will mean any processes it spawns will also have elevated privileges. Is it an option for you to just launch PowerShell as admin? – G42 Oct 03 '17 at 09:19
  • You can use option `/savecred` so the password has to be entered only the first time. But it will always open another window. –  Oct 03 '17 at 09:31
  • @gms0ulman how to launch powershell as admin? – user1559625 Oct 03 '17 at 09:32
  • 1
    EC2 userdata is an option: http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html – henrycarteruk Oct 03 '17 at 09:34
  • 1
    @user1559625 [Check out this StackOverflow post](https://stackoverflow.com/a/31602095/5039142). There are multiple ways this could be done. How is the batch file being automatically run? There are other ways to do this in Task Scheduler, or if you have a shortcut in the StartUp folder. – G42 Oct 03 '17 at 10:51

0 Answers0