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
standardoutput
and StandardError
from different threadsOutputDataReceived
and ErrorDataReceived
events, to avoid using extra threadsCan 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
#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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP