【问题标题】:Powershell Exception issuePowershell异常问题
【发布时间】: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


【解决方案1】:

您可以通过将 ErrorAction 添加到 Get-Process 来忽略您没有所需访问权限的进程

Get-Process -FileVersionInfo -ErrorAction Ignore

如果您确实需要知道哪些流程给您带来了错误,您可以使用

Get-Process -FileVersionInfo -ErrorAction SilentlyContinue

然后查看$errorcudo's to @zett42

Get-Process -FileVersionInfo -ErrorAction SilentlyContinue -ErrorVariable ProcError

然后看看$ProcError

【讨论】:

  • @zett42 - 谢谢,已更新
猜你喜欢
  • 2022-12-06
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 2018-06-07
相关资源
最近更新 更多