0

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!

scapegoat17
  • 5,509
  • 14
  • 55
  • 90
  • 2
    You can't really circumvent UAC without completely turning it off (if you do figure out a way, file a bug with Microsoft and you'll probably get some nice cash as part of their bug bounty). Even if you do impersonate as an Administrator, they aren't in an elevated context. You can do "tricks" to make this work, like having a Windows Service run as SYSTEM, and then pupating the Windows Service through commands, but doing that securely is hard. – vcsjones Nov 12 '13 at 14:43

1 Answers1

1

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.

MDMoore313
  • 3,233
  • 1
  • 23
  • 38
  • It's not that I necessarily want to disable UAC if I don't have to. The commands in the script that I am running require to be run as an administrator. If you still recommend that the only way I would be able to do this is by disabling UAC, then how would I go about doing so? – scapegoat17 Nov 12 '13 at 14:49
  • By `Run as an administrator` you mean the script requires elevated rights, correct? – MDMoore313 Nov 12 '13 at 15:04
  • I am really sorry, but I am not sure what you mean by "elevated rights". I know that if I run Powershell as an Administrator it does not prompt me with anything when I run my script - which is what I need in my situation. If I run Powershell as normal, it comes up with the UAC prompt. And the script gets kicked off through a C# Windwos Service. So i figured that I would some how need a way to make it kickoff through a run as Administrator, but if only it were that simple :) – scapegoat17 Nov 12 '13 at 15:20
  • @scapegoat17 see this [question](http://stackoverflow.com/questions/8986971/what-precisely-does-run-as-administrator-do) – MDMoore313 Nov 12 '13 at 15:26
  • So that mean that I would need to have my user account as an administrator in order to run the script without UAC getting in the way? – scapegoat17 Nov 12 '13 at 15:35