預先感謝您考慮到這種愚蠢的查詢。我正在嘗試使用以下查詢從 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”行程的檔案版本資訊。在行:1 字符:7
- 嘗試 { Get-Process -FileVersionInfo | 選擇-唯一| 選擇物件 * ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : PermissionDenied: (System.Diagnostics.Process (csrss):Process) [Get-Process], ProcessCommandException
- FullQualifiedErrorId:CouldnotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand
試圖獲取例外詳細資訊
**$Error[0] | Select-Property ***
和
**$Error[0].exception.GetType().fullname**
這給出了以下結果:
WriteErrorStream : True PSMessageDetails : 例外
:Microsoft.PowerShell.Commands.ProcessCommandException:無法列舉“空閑”行程的檔案版本資訊。---> System.ComponentModel.Win32Exception: 無法列舉行程模塊。在 System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly) 在 System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId) 在 System.Diagnostics.Process.get_MainModule() 在 System.Management.UtilProcess(targetMaindModule()) ) 在 Microsoft.PowerShell.Commands.GetProcessCommand.ProcessRecord() --- 內部例外堆疊跟蹤結束 --- TargetObject : System.Diagnostics.Process (Idle) CategoryInfo : PermissionDenied: (System.Diagnostics.Process (Idle):Process ) [Get-Process], ProcessCommandExceptionfullyQualifiedErrorId : 無法列舉檔案Ver,
在嘗試使用 [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
}
你能幫忙嗎?提前致謝。
uj5u.com熱心網友回復:
您可以通過向 Get-Process 添加 ErrorAction 來忽略您沒有所需訪問權限的行程
Get-Process -FileVersionInfo -ErrorAction Ignore
如果您確實需要知道哪些行程給了您錯誤,您可以使用
Get-Process -FileVersionInfo -ErrorAction SilentlyContinue
然后查看$error或cudo 到 @zett42
Get-Process -FileVersionInfo -ErrorAction SilentlyContinue -ErrorVariable ProcError
并期待在$ProcError之后
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345211.html
