我嘗試過Get-Process
獲取Get-CimInstance Win32_Process
GPU 使用情況和 GPU 內存,但它們都無法提供這些資訊。
在Windows任務管理器中,它可以顯示GPU記憶體。
那麼,有沒有辦法讓這些進入powershell
答案1
Windows 使用性能計數器來追蹤這類事情。您可以透過 取得 powershell 中的值Get-Counter
。根據您的 GPU 和驅動程序,您可能有更具體的可用計數器,但這些範例應該始終可用:
# Example to get GPU usage counters for a specific process:
$p = Get-Process dwm
((Get-Counter "\GPU Process Memory(pid_$($p.id)*)\Local Usage").CounterSamples | where CookedValue).CookedValue |
foreach {Write-Output "Process $($P.Name) GPU Process Memory $([math]::Round($_/1MB,2)) MB"}
((Get-Counter "\GPU Engine(pid_$($p.id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue |
foreach {Write-Output "Process $($P.Name) GPU Engine Usage $([math]::Round($_,2))%"}
# Outputs:
Process dwm GPU Process Memory 259.36 MB
Process dwm GPU Engine Usage 0.47%
# Example to get total GPU usage counters:
$GpuMemTotal = (((Get-Counter "\GPU Process Memory(*)\Local Usage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
Write-Output "Total GPU Process Memory Local Usage: $([math]::Round($GpuMemTotal/1MB,2)) MB"
$GpuUseTotal = (((Get-Counter "\GPU Engine(*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
Write-Output "Total GPU Engine Usage: $([math]::Round($GpuUseTotal,2))%"
# Outputs:
Total GPU Process Memory Local Usage: 511.36 MB
Total GPU Engine Usage: 0.77%
我通過運行找到了這些計數器名稱Get-Counter -ListSet 'GPU*'
,並Counter
從那裡獲取值。看起來 GPU 引擎的使用分為 4 種類型:
engtype_3D
engtype_VideoDecode
engtype_Copy
engtype_VideoProcessing
我只是engtype_3D
在範例中進行總結,因為它是我的系統上唯一使用的範例。這些值都與 Windows 工作管理員中顯示的值相匹配,因此應該足夠準確。