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!
CodePudding user response:
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. UnlikeSilentlyContinue
,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"})}