TransWikia.com

Process.StandardOutput.ReadToEnd() is always Empty

Stack Overflow Asked on December 1, 2021

I am executing auditpol /get /category:* as another user, but I always get an empty result on $GetProcessResult. Auditpol requires administrative privileges before it can be ran. I have found similar questions, and the recommendations I got were to

  • get the standardoutput and StandardError from different threads
  • subscribe to the OutputDataReceived and ErrorDataReceived events, to avoid using extra threads

Can you provide examples of the solutions above?

I am very new to PowerShell, and still learning.

Here is the code:


#Get UserB credential
$Credential = Get-Credential garuwindowscit
#Use System.Diagnostics to start the process as UserB
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo

#With FileName we're basically telling powershell to run another powershell process
$ProcessInfo.FileName = "powershell.exe"
$ProcessInfo.CreateNoWindow = $true
$ProcessInfo.Verb = "runas"
#CreateNoWindow helps avoiding a second window to appear whilst the process runs

#Note the line below contains the Working Directory where the script will start from
$ProcessInfo.WorkingDirectory = $env:windir
$ProcessInfo.RedirectStandardError = $true 
$ProcessInfo.RedirectStandardOutput = $true 
$ProcessInfo.UseShellExecute = $false

#The line below is basically the command you want to run and it's passed as text, as an argument
$ProcessInfo.Arguments = "auditpol /get /category:*"

#The next 3 lines are the credential for UserB, as you can see, we can't just pass $Credential
$ProcessInfo.Username = $Credential.GetNetworkCredential().username
$ProcessInfo.Domain = $Credential.GetNetworkCredential().Domain
$ProcessInfo.Password = $Credential.Password

#Finally start the process and wait for it to finish
$Process.StartInfo = $ProcessInfo 
$Process.Start() | Out-Null 

$Process.WaitForExit() 

#Grab the output
$GetProcessResult = $Process.StandardOutput.ReadToEnd()

#Print the Job results
$GetProcessResult

One Answer

#Get UserB credential
$Credential = Get-Credential garuwindowscit
#Use System.Diagnostics to start the process as UserB
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo

#With FileName we're basically telling powershell to run another powershell process
$ProcessInfo.FileName = "powershell.exe"
$ProcessInfo.CreateNoWindow = $true
$ProcessInfo.Verb = "runas"
#CreateNoWindow helps avoiding a second window to appear whilst the process runs

#Note the line below contains the Working Directory where the script will start from
$ProcessInfo.WorkingDirectory = $env:windir
$ProcessInfo.RedirectStandardError = $true 
$ProcessInfo.RedirectStandardOutput = $true 
$ProcessInfo.UseShellExecute = $false

#The line below is basically the command you want to run and it's passed as text, as an argument
$ProcessInfo.Arguments = "auditpol /get /category:*"

#The next 3 lines are the credential for UserB, as you can see, we can't just pass $Credential
$ProcessInfo.Username = $Credential.GetNetworkCredential().username
$ProcessInfo.Domain = $Credential.GetNetworkCredential().Domain
$ProcessInfo.Password = $Credential.Password

#Finally start the process and wait for it to finish
$Process.StartInfo = $ProcessInfo 
$Process.Start() | Out-Null 

#Grab the output
$GetProcessResult = $process.standardoutput.readtoend()
$GetProcessResult

$Process.WaitForExit()

You should wait the process for exit after you have received the output, otherwise it will redirect no data.

Answered by programmer365 on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP