Recently I ran into an interesting topic. I need to run a powershell command as Administrator
PowerShell.exe -NoProfile -Command "Start-Process PowerShell
-Verb RunAs -Args $command -PassThru -Wait"
and get stdout of this command. Sadly, it gives me only process object.
I tries methods described here:
- Redirecting Output on Child Process to Parent Process - Powershell
- Capturing standard out and error with Start-Process
- Powershell: Capturing standard out and error with Process object
But somehow code
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "ipconfig.exe"
$pinfo.Arguments = "/all"
$pinfo.CreateNoWindow = $true
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Verb = "RunAs"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
keeps ignoring $pinfo.Verb = "RunAs"
string and runs as regular user.
Please note, that I need to be able to run powershell commands with elevated privileges, not just programs like ping and calc.
Any ideas how to fix this? Thank you.