How to run cmd or powershell command from golang as admin? - powershell

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

Related

PowerShell command not run via cmd script

Here see the script, the last command should works, i mean everything's ok and it works when i run in windows Powershell.
But when i join that with 2 first commands which are bash scripts, the powershell's one won't run:
Net user administrator /active:yes
Net user administrator Hardware123
Set-LocalUser -Name "administrator" -PasswordNeverExpires 1
If you want to call powershell commands from a cmd / batch file, use the following syntax. You have to call powershell to be able to run powershell commands,
Net user administrator /active:yes
Net user administrator Hardware123
powershell.exe Set-LocalUser -Name "administrator" -PasswordNeverExpires 1
This is with assumption that you already have admin access to perform the above script.
You can also run multiple commands with powershell.exe if you intend to use variables,
powershell -Command "$local = Get-LocalUser; $local.Name; if ('Administrator' -in $local.Name) { 'Do Something' } else { 'Administrator Not Found' }"

Passing commands to cmd from powershell

I want to make changes to a registry key through this command:
REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f
This has to happen in a cmd, which i ran as administrator through powershell with this command in a batch file:
powershell.exe Start-Process cmd.exe -Verb runAs
I need a UAC Prompt for the user to input his admin credentials to make it as user friendly as possible.
Now my question: How do i pass the reg add command to the console which i started as administrator?
You need to pass your command in -ArgumentList parameter like this:
powershell.exe "Start-Process powershell -ArgumentList 'REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f' -Verb RunAs"
This will execute PowerShell which tries to execute another PowerShell window asking you for credentials and then execute REG ADD command and close PowerShell at the end.
Keep in mind that you don't have error handling or anything like this so you may want to add them later as they might be very useful.

Run powershell script from cmd with admin privileges

I am running a powershell script from cmd and I want to run it with admin rights.
powershell -command "&{. D:\Temp\Untitled1.ps1 ; Method1 'argument1'}"
Since I am not using start-process so I cannot use -verb Runas, what are the other work arounds for the same?

Executing Powershell script from command prompt without changing environment?

I am writing a power shell script to execute one sample script. But when i am running it from command prompt it is changing environment from command prompt to Power shell and executing script. Which i don't want .
Powershell.exe -executionpolicy remotesigned -File C:\Users\ic020511\Desktop\Script.ps1
Is there any command that would not switch from command prompt and execute script
You should be able to do that using the start command:
start Powershell.exe -windowstyle hidden -executionpolicy remotesigned -File C:\Users\ic020511\Desktop\Script.ps1

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd