0

My code looks like this:

Start-Transcript -Path "C:/test.txt" Write-Host (Get-Date).ToString("yyyy-MM-dd_HH:mm:ss") "Starting" $processes_using_oci_dll = Get-Process | ? { (get-process -id $_.id -module | ? {$_.filename -like "*\oci.dll"})} 2> $null Write-Host (Get-Date).ToString("yyyy-MM-dd_HH:mm:ss") "Ending" 

When watching its output through a powershell cli, it looks nice, the Get-Process ... 2> $null avoids the permission issues from being displayed on screen.

Nevertheless, when I open the file C:/test.txt, I can see a huge amount of entries such as

+ ... dll = Get-Process | ? { (get-process -id $_.id -module | ? {$_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (System.Diagnost... (aesm_service):Process) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : CouldnotEnumerateModules,Microsoft.PowerShell.Commands.GetProcessCommand get-process : No se pueden enumerar los módulos del proceso "REDACTED". 

My question is: How can I effectively keep these warnings from being Transcripted to the file C:/test.txt?

Thanks in advance!

1
  • @zett42 it works like a charm! What about posting your comment as an answer, so I can accept it and upvote it? Commented Aug 3, 2022 at 7:42

1 Answer 1

1

Apart from redirecting the error stream (#2) to $null, you can also use the -ErrorAction (alias -EA) common parameter, which is more readable and more efficient, as the error is suppressed at the source. It also gives you additional options, as follows:

  • -ErrorAction SilentlyContinue suppresses the error message and continues executing the command.
  • -ErrorAction Ignore suppresses the error message and continues executing the command. Unlike SilentlyContinue, Ignore doesn't add the error message to the $Error automatic variable.
$processes_using_oci_dll = Get-Process -EA Ignore | ? { (get-process -id $_.id -module -EA Ignore | ? {$_.filename -like "*\oci.dll"})} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.