Thank you in advance to taking in consideration such stupid query. I am trying to get Process list with some extended properties from powershell with following query:
**Get-Process -FileVersionInfo | select -Unique | Select-Object * | Format-Table -Property OriginalFilename, FileName, InternalName, ProductName, CompanyName, FileVersion -Wrap > C:\Users\user\OneDrive\Desktop\final.txt**
It works but for some processes I am unable to get FileVersion which is ok and I don't care about that. Problem is that Even trying to catch the exception, It simple does nothing.
Get-Process: Cannot enumerate the file version information of the "csrss" process. At line:1 char:7
- try { Get-Process -FileVersionInfo | select -Unique | Select-Object * ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : PermissionDenied: (System.Diagnostics.Process (csrss):Process) [Get-Process], ProcessCommandException
- FullyQualifiedErrorId : CouldnotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand
Tried to get exception details with
**$Error[0] | Select-Property ***
and
**$Error[0].exception.GetType().fullname**
Which gives following result:
WriteErrorStream : True PSMessageDetails : Exception
: Microsoft.PowerShell.Commands.ProcessCommandException: Cannot enumerate the file version information of the "Idle" process. ---> System.ComponentModel.Win32Exception: Unable to enumerate the process modules. at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly) at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId) at System.Diagnostics.Process.get_MainModule() at System.Management.Automation.PsUtils.GetMainModule(Process targetProcess) at Microsoft.PowerShell.Commands.GetProcessCommand.ProcessRecord() --- End of inner exception stack trace --- TargetObject : System.Diagnostics.Process (Idle) CategoryInfo : PermissionDenied: (System.Diagnostics.Process (Idle):Process) [Get-Process], ProcessCommandException FullyQualifiedErrorId : CouldnotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at , : line 1 PipelineIterationInfo : {0, 1, 64, 0...}
While trying to catch the exception with [Microsoft.PowerShell.Commands.ProcessCommandException] it simply does nothing and still throws bunch on red lines.
try { Get-Process -FileVersionInfo | select -Unique | Select-Object * | Format-Table -Property OriginalFilename, FileName, InternalName, ProductName, CompanyName, FileVersion -Wrap > C:\Users\user\OneDrive\Desktop\final.txt
} catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
Write-Verbose "Catch all" -Verbose
}
Could you help please? Thanks in advance.
CodePudding user response:
You can ignore the processes you don't have the required access to by adding an ErrorAction to Get-Process
Get-Process -FileVersionInfo -ErrorAction Ignore
If you somehow do need to know what processes gave you errors, you can use
Get-Process -FileVersionInfo -ErrorAction SilentlyContinue
and look at $error
afterwards or cudo's to @zett42
Get-Process -FileVersionInfo -ErrorAction SilentlyContinue -ErrorVariable ProcError
and look at $ProcError
afterwards