Search code examples
windowspowershellgouac

How do I start a powershell process with administrator privilges and redirect stdin (os.exec)


I want to start a powershell process with administrator priviliges and redirect stdin to a writer, I have everything working except how to run it as admin.

// Start powershell
powershell := exec.Command("powershell")
// Add something that will open UAC and give the process admin priviliges

powershell.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
powershell.Env = os.Environ()

// Create pipe to write to it later
stdin, err := powershell.StdinPipe()
if err != nil {
    log.Fatal(err)
}

defer stdin.Close()

As it seems there is no such thing as Process.Verb that I can set to "runas", I tried creating a powershell process by using powershell commands Start-Process ... -Verb runas and got the pid of the process successfully, but it seems like there's no way to manipulate stdin and stdout later with a pid.


Solution

  • Thanks to @jeroen-mostert I kinda figured it out. I started my go executeable as admin and used StartProcess powershell.exe -verb RunAs, it isn't working like I wanted in the beginning, but at least it doesnt open UAC for powershell anymore.