Execute powershell command with elevated rights and output error messages

Welcome Forums General PowerShell Q&A Execute powershell command with elevated rights and output error messages

Viewing 6 reply threads
  • Author
    Posts
    • #190357
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member

      The following code works. But I don’t get any error codes from the called process

      Start-Process powershell.exe -Verb runas -ArgumentList "$Command" -Wait -PassThru

      The following code returns the error messages from the called process. Powershell does not seem to be running elevated rights despiteĀ  $oProcessInfo.Verb = “RunAs”.

      $oProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
      $oProcessInfo.FileName = $sProcess
      $oProcessInfo.Verb = "RunAs"
      $oProcessInfo.RedirectStandardError = $true
      $oProcessInfo.RedirectStandardOutput = $true
      $oProcessInfo.UseShellExecute = $false
      $oProcessInfo.Arguments = $sArgs
      $oProcessInfo.CreateNoWindow = $true
      $oProcess = New-Object System.Diagnostics.Process
      $oProcess.StartInfo = $oProcessInfo
      $oProcess.Start() | Out-Null
      $oProcess.WaitForExit() | Out-Null
      $sSTDOUT = $oProcess.StandardOutput.ReadToEnd()
      $sSTDERR = $oProcess.StandardError.ReadToEnd()
      $pOutPut.Value="Commandline: $sProcess $sArgs`r`n"
      $pOutPut.Value+="STDOUT: " + $sSTDOUT + "`r`n"
      $pOutPut.Value+="STDERR: " + $sSTDERR + "`r`n"

      Does anyone have an idea what I’m doing wrong or how can I solve my problem?

    • #190360
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      you can use -RedirectStandardError parameter with Start-Process, it accepts a file name where it will write the error. Similarly for -RedirectStandardOutput

    • #190363
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member

      Can I spend that directly or do I have to go through a file export?

    • #190378
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      You could have read the help for Start-Process and simply try it. :-/

    • #190474
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member

      In the help I find the following:

      • Specifies a file.
      • By default, the errors are displayed in the console.

      If I do not specify a parameter after -RedirectStandardOutput I get a error message, Enter a parameter of type “System.String”.

      If I pass a variable behind it, it will not be filled. So where is the mistake? Or, how is the Help to understand?

    • #190483
      Participant
      Topics: 5
      Replies: 9
      Points: 74
      Rank: Member

      The combination -Verb runas and -RedirectStandardOutput C: \temp\Error.txt also results in error:

      The parameter set can not be resolved with the given named parameters

      I Tryed:

      start-Process powershell.exe -Verb runas -ArgumentList "$Command" -Wait -PassThru -RedirectStandardOutput C:\temp\Error.txt
    • #190552
      Participant
      Topics: 6
      Replies: 23
      Points: 104
      Helping Hand
      Rank: Participant
Viewing 6 reply threads
  • The topic ‘Execute powershell command with elevated rights and output error messages’ is closed to new replies.