我有兩個相同的運行行程稱為 RocketLeague.exe
我想通過匹配兩個行程的完整檔案路徑,將其中一個行程 PID 存盤在一個變數中,以便以后在另一個命令中使用。
到目前為止,我已經能夠提出兩個命令來傳輸行程的完整路徑,但無法弄清楚如何將正確的 PID 繼續傳輸到最終的自定義命令中。
如何將正確的 PID 存盤在變數中以用于我的自定義命令?
1) Get-Process -Name 'RocketLeague' | Format-List -Property Path
2) Get-Process -Name 'RocketLeague'
uj5u.com熱心網友回復:
使用來自user:lit我的反饋,我能夠想出這個解決方案。
$procID = Get-process -Name 'RocketLeague' | Select-Object -Property Id,Path | ForEach-Object {
If($_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'){
Set-Variable -Name 'procSteam' -Value $_.Id; Write-Host $procSteam
}
}
uj5u.com熱心網友回復:
如果您只想要與該路徑相同的特定行程,您可以使用Where-Object或.Where()過濾方法。代碼將簡化為:
# This:
$procID = (Get-Process -Name 'RocketLeague').Where({
$_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'
}) | Select-Object -Property Id, Path
# Or this:
$procID = Get-Process -Name 'RocketLeague' | Where-Object {
$_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'
} | Select-Object -Property Id, Path
例如,如果只有一個以Win64\....exe您結尾的路徑,則可以使用.EndsWith()方法:
$procID = (Get-Process -Name 'RocketLeague').Where({
([string]$_.Path).EndsWith('\Win64\RocketLeague.exe')
}) | Select-Object -Property Id, Path
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379956.html
上一篇:無法將“System.Char[,]”型別的“System.Char[,]”值轉換為“System.Char”型別
下一篇:命令控制臺輸出-到檔案。獲取{WindowsFilteringPlatform/WindowsFirewall}規則
