简体   繁体   中英

Run a Powershell Script as Administrator without any kind of prompt

I need to run a Powershell script completely automated, but it needs to be run as an administrator. I am open to doing this with either Powershell commads or C# coding. Which ever provides me the answer first. I have been looking around online for what seems like forever, but I am still unsuccessful...

For C# I have tried this, but do not have a domain name and it seems to throw errors: http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

For Powershell I have tried using Start-Process -credential "username" but there doesn't seem to be a way for me to enter the password portion and if i try to do:

Start-Process -credential "username", "Password"

or

Start-Process -credential "username" "Password"

I get an error saying Start-Process : Cannot process argument transformation on parameter 'Credential'.

I have also tried using Start-Process Powershell -Verb runAs , but that still brings up UAC and I would still need to run the window in the background (which isn't my main concern since i can just use -NoNewWindow for that).

I am relatively new with Powershell and it would be really helpful if anybody had any ideas as to how I would go about doing this.

Thank you!

Running as an administrator won't solve much if the script isn't running elevated , which is what -RunAs does. If you don't want it to prompt at all then disable UAC, even running the script as a user that's a member of the Administrators group will still prompt if UAC isn't disabled, or your script will fail if it isn't running with elevated privileges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM