如何将所有与某个进程名称匹配的 Get-Process 进程一一传递给另一个 PowerShell 脚本?
伪代码
for each process in matchingprocesses:
myScript(process)
计划使用 Set-WorkingSetToMin 脚本:https ://www.powershellgallery.com/packages/PoshInternals/1.0/Content/Set-WorkingSetToMin.ps1
这很好用,因为只有一个 notepad++ 进程:
get-process notepad++ | Set-WorkingSetToMin
但是对于 VS Code,这只获取第一个代码进程并忽略其余部分:
get-process code | Set-WorkingSetToMin
如何将匹配某个名称的每个进程通过管道传输到 powershell 脚本?
替代方法是修改 PoshInternals 脚本以接受多个进程:
# Dont run Set-WorkingSet on sqlservr.exe, store.exe and similar processes
# Todo: Check process name and filter
# Example - get-process notepad | Set-WorkingSetToMin
Function Set-WorkingSetToMin {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$True, Mandatory=$true)]
[System.Diagnostics.Process] $Process
)
if ($Process -ne $Null)
{
$handle = $Process.Handle
$from = ($process.WorkingSet/1MB)
$to = [PoshInternals.Kernel32]::SetProcessWorkingSetSize($handle,-1,-1) | Out-Null
Write-Output "Trimming Working Set Values from: $from"
} #End of If
} # End of Function
在没有额外变量的情况下在一行中回答:
foreach ($process in Get-Process myprocessname) { Set-WorkingSetToMin ($process) }