0

How can cmd and powershell commands be executed from Go with admin privilege (run as admin)? Note that the execution of cmd and powershell with admin privilege is clear, but how to specify that the commands executed by them also have admin privilege is questionable. For example how can we run this powershell command in go as administrative:

Stop-Process -ID 11111 -Force

or this command:

taskkill /IM example.exe /F
sh.sagheb
  • 27
  • 1
  • 1
  • 7
  • 1
    Does this answer your question? [Running external commands through os/exec under another user](https://stackoverflow.com/questions/21705950/running-external-commands-through-os-exec-under-another-user) – Raya Dec 07 '22 at 11:43
  • @Raya As I understand from this link, this is how to run a program (here PowerShell) from within the cmd as admin or to run the cmd from within PowerShell as admin. But my question is how to execute the command in powershell or the command in cmd as admin? – sh.sagheb Dec 07 '22 at 11:51
  • If you're running either `cmd` or `powershell` with administrative privileges, they will be elevated and their commands will also run with administrative privileges. For this to happen from Go, your Go process would *also* need to elevate itself (or more accurately, request elevation on launch or start a subprocess with that), otherwise it wouldn't be able to pass commands (if it could do so, that would be a security hole). – Jeroen Mostert Dec 07 '22 at 11:56
  • @JeroenMostert The problem is that it is not possible to declare in the form of one command (at the same time) that both the execution of PowerShell and the command that we want to execute in PowerShell are as admin. In fact, when we first say run powershell through cmd as admin and then we want to run a powershell command as the next command of the program, the second command will not be run in the powershell which was run as admin in the previous command. example in next comment... – sh.sagheb Dec 07 '22 at 12:19
  • ‍‍‍‍```command := "powershell start-process powershell -verb runas"--- cmd2 := exec.Command(command)--- err3 := cmd2.Run()--- gosh.PowershellCommand(string("Stop-Process -ID 11111 -Force")).‍‍‍``` If my conclusion is wrong, I would be grateful if you could explain on this example. – sh.sagheb Dec 07 '22 at 12:20
  • No, that's correct, and is exactly how things should work. Your main process is not elevated and is not permitted to pass commands to this new, elevated process; if it were capable of doing so, so would any other non-elevated process. Either package your commands as a script, or use `-Command` to tell PowerShell what to do directly (e.g. `powershell start-process -verb runas powershell '-command dir c:\; pause'`). – Jeroen Mostert Dec 07 '22 at 12:26
  • @JeroenMostert Thanks, I have tried these instructions, ذut this PowerShell command is still not executed: ‍‍‍‍‍```command := "powershell start-process -verb runas powershell '-command Stop-Process -ID 11111 -Force'" ..... cmd2 := exec.Command(command).....err3 := cmd2.Run()```. – sh.sagheb Dec 07 '22 at 13:01

0 Answers0