【发布时间】:2021-11-02 10:10:49
【问题描述】:
提前感谢您考虑这种愚蠢的查询。 我正在尝试通过以下查询从 powershell 获取具有一些扩展属性的进程列表:
**Get-Process -FileVersionInfo | select -Unique | Select-Object * | Format-Table -Property OriginalFilename, FileName, InternalName, ProductName, CompanyName, FileVersion -Wrap > C:\Users\user\OneDrive\Desktop\final.txt**
它可以工作,但对于某些进程,我无法获得 FileVersion,这没问题,我不在乎。问题是即使试图捕获异常,它也无济于事。
Get-Process: 无法枚举的文件版本信息 “csrss”过程。在 line:1 char:7
- 尝试 { Get-Process -FileVersionInfo |选择-唯一 |选择对象 * ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : PermissionDenied: (System.Diagnostics.Process (csrss):Process) [Get-Process], ProcessCommandException
- FullyQualifiedErrorId : CouldnotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand
尝试使用
获取异常详细信息**$Error[0] | Select-Property ***
和
**$Error[0].exception.GetType().fullname**
结果如下:
WriteErrorStream:真 PSMessageDetails:异常
:Microsoft.PowerShell.Commands.ProcessCommandException:不能 枚举“Idle”的文件版本信息 过程。 ---> System.ComponentModel.Win32Exception: 无法枚举进程 模块。 在 System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 进程 ID, Boolean firstModuleOnly) 在 System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 进程 ID) 在 System.Diagnostics.Process.get_MainModule() 在 System.Management.Automation.PsUtils.GetMainModule(进程 目标进程) 在 Microsoft.PowerShell.Commands.GetProcessCommand.ProcessRecord() --- 内部异常堆栈跟踪结束 --- TargetObject : System.Diagnostics.Process (Idle) CategoryInfo : PermissionDenied: (System.Diagnostics.Process (Idle):Process) [Get-Process],ProcessCommandException FullyQualifiedErrorId : 无法枚举文件版本,Microsoft.PowerShell.Commands.GetProcessCommand 错误详细信息:调用信息: System.Management.Automation.InvocationInfo ScriptStackTrace:在 , : 第 1 行 PipelineIterationInfo : {0, 1, 64, 0...}
在尝试使用 [Microsoft.PowerShell.Commands.ProcessCommandException] 捕获异常时,它什么也不做,仍然在红线上抛出一堆。
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
}
你能帮忙吗? 提前致谢。
【问题讨论】:
-
在 try 块中使用
-ErrorAction Stop! -
您好@AbdulNiyasPM,感谢您的回复。你这是什么意思?
标签: powershell try-catch