这是一个执行此操作的 power shell 脚本。如果需要,您还可以使用其他 bat 文件运行它。然后在任务管理器中手动设置基准的亲和力。
run_set_affinity.bat:
powershell -executionpolicy bypass -file set_affinity.ps1
set_affinity.ps1:
# elevate privileges if we are not running as Administrator, so we can set affinity of Windows owned processes
# source: http://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
'tried to elevate to full privileges, did not work, aborting'
} else {
'running my self again with full privileges'
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-executionpolicy bypass -noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
'running with full privileges'
# set affinity of all processes to CPU 3 and CPU 4
# it prints processes that it was unable to set affinity of
# source: https://digitaljive.wordpress.com/2011/11/18/set-processor-affinity-with-powershell/
# 1 (CPU 1)
# 2 (CPU 2)
# 4 (CPU 3)
# 8 (CPU 4)
# 16 (CPU 5)
# 32 (CPU 6)
# 64 (CPU 7)
# 128 (CPU 8)
$affinity = 4 + 8
'setting all processes to affinity: '+$affinity
'processes unable to set affinity of: '
$allProcesses = Get-Process *
foreach ($process in $allProcesses) {
try {
$process.ProcessorAffinity = $affinity
}
catch {
$process
}
}