Windows CMD Batch File Run A Command as Administrator
Asked Answered
A

1

3

I have a batch file where I want to run some commands, but not all of them as administrator. I do not want to run the command prompt as administrator or run the entire batch job as administrator. An example of a command that I want to run as administrator is:

appcmd.exe recycle apppool \apppool.name -DefaultApp

Appcmd is the command for managing IIS 7, and this example I want to recycle an App Pool.
I have tried:

runas /profile /user:MYPC\administrator "appcmd.exe recycle apppool \apppool.name -DefaultApp"

I get a 1326: Logon failure: unknown user or bad password, but I know the user and password exist. Any suggestions as to how I can do this?

Alannaalano answered 10/7, 2012 at 13:53 Comment(0)
C
1

I found that running runas without the /profile parameter worked fine. But if you must run it with /profile then you could execute this command just prior:

runas /profile /user:MYPC\administrator "cmd"

Which will unfortunately prompt you twice.

Certify answered 26/7, 2012 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.