PowerShell 3.0 - 为每个用户的进程设置 CPU 的关联



我在这里的第一篇文章。我正在使用powerShell编写脚本,目标是为每个用户的进程设置一定数量的cpu线程,使用这里的论坛,我能够找到大多数答案,甚至让我的脚本运行,除了,如果它设置亲和性,它将其设置为每个进程,而不仅仅是我需要的用户。下面是代码(带注释):

# GET LIST of all process running
$pList = get-wmiobject win32_process
# loop through created array and get the OWNER of the processes
foreach ($p in $pList) {
    #If "myUserName" is found:
    if ($p.getowner().User -eq 'myUserName') {
        # get process name
        $procName = $p.ProcessName
        # trim STRING to remove EXE
        $procName = $procName.Replace('.exe','')
        # use get-process to make array of processes run by "myUserName"
        $activeProc = Get-Process -name $procName
        # Loop to set affinity for each process
        foreach ($i in $activeProc){
            $i.ProcessorAffinity=0xFE
        }
    }
}

当我执行这个命令时,所有的进程都被设置为新的线程数,任何建议如何使它只调整线程为特定的用户?

谢谢大家!这件事很紧急。

通过调用get-process -name $procName,您将发现所有与用户运行的进程具有相同的名称

ProcessId代替ProcessName

在PowerShell 4.0版本中,您可以在Get-Process cmdlet上使用-IncludeUserName参数。一旦您有了一个进程列表,您就可以使用Where-Object cmdlet对它们进行过滤,它的默认别名是?

Get-Process -IncludeUserName | Where-Object -FilterScript { $PSItem.UserName -match 'system' };

或者简写可能像这样:

gps -inc | ? { $_.UserName -match 'system' };

注意:使用-IncludeUserName参数需要提升权限

最新更新