Powershell – Setting Processor Affinity or Priority

Processor Affinity (to keep it simple, because that’s how I think) is the ability to delegate a SPECIFIC core to an application.
You see by default, if an application in Windows supports multiple cores, it will try to share and work with all of them. Which is fine 80% of them.
The other 20% that application is a resource monster (Like a Virtualized Machine on a laptop) and you need to cap it’s power without killing it.
Enter Processor Affinity. On a Multi Core Server or Workstation I can allocate just ONE core (or more) to an application. Usually it’ll be the case that I’m taking away cores.

Get-Process processname | Select-Object ProcessorAffinity

or

(ps processname).processoraffinity

Remember it’s a Binary value you’re computing so to figure out the Binary number to use, you must add it in your head or draw it out.

0001 = 1 ( CPU 1)
0010 = 2 ( CPU 2)
0100 = 4 ( CPU 3 )
1000 = 8 ( CPU 4 )

So, you'll change any applaction value like these:

(ps processname).processoraffinity=value

Of course, you may modify the priority of any program too:

(ps processname).priorityclass="char"
$char=normal,belownormal,idle...

PowerShell Scripts Example:
Effect: set affinity for same name processes

get-process  processes_name | format-table id -autosize | out-file -filepath c:\test.txt
$b=(get-content c:\test.txt)[3..1000]
$num=$b.count
$a=(get-content c:\test.txt)[3..$num]
foreach($i in $a){(get-process -id $i).processoraffinity=8}
remove-item c:\test.txt

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: