I think you have the idea of processor affinity wrong here. He's a snippet from the MSDN:
Assign processor affinity to better use CPU caching
To efficiently use CPU caches on multiprocessor servers, you can configure application pools to establish affinity between worker processes and the processors. Processor affinity forces worker processes to run on a specific microprocessor or CPU, and applies to all worker processes serving a particular application pool. You can also use processor affinity with Web gardens that run on multiprocessor computers where clusters of CPUs are dedicated to specific application pools.
Controlling processor affinity can improve performance by reducing the number of processor cache flushes as threads move from one processor to another. For example, you might be able to reduce cache flushes for dedicated servers by assigning processor affinity. However, the performance trade-off is that dedicating a program to a particular processor might prevent other program threads from migrating to the least-busy processor.
By default, a process' affinity is set to all available CPUs, so if you dedicate a particular CPU to a particular potentially-complex process, can you improve performance because the CPU cache is not reset as often for that process as it would be if its threads were permitted to move from one core to another.
If you run this script in ISE, you will notice that the affinity for the powershell_ise.exe process is set to CPU01 (assuming you have a multi-core processor). You will also notice that if you spawn new instances of powershell.exe, the affinities for those processes are also set to CPU01:
function Main
{
Wait-Event -SourceIdentifier ManagementEventWatcher.EventArrived
Main
}
$type = @"
using System;
[Flags]
public enum CpuCores
{
Zero = 1,
One,
Two,
Three,
Four,
Five,
Six,
Seven
}
"@
Add-Type -TypeDefinition $type
$processorAffinity = [IntPtr][Int32]([CpuCores]::One) # Set your processor affinity here
[System.Diagnostics.Process]::GetCurrentProcess().ProcessorAffinity = $processorAffinity
$eventClassName = "__InstanceCreationEvent"
$withinInterval = New-Timespan -Seconds 1
$condition = "TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='powershell.exe'"
$query = New-Object System.Management.WqlEventQuery -ArgumentList $eventClassName, $withinInterval, $condition
$processWatcher = New-Object System.Management.ManagementEventWatcher -ArgumentList $query
Register-ObjectEvent -InputObject $processWatcher -EventName EventArrived -SourceIdentifier ManagementEventWatcher.EventArrived -Action {
param (
[System.Object]$sender,
[System.Management.EventArrivedEventArgs]$e
)
New-Event -SourceIdentifier PowerShellProcess.Created -Sender $sender -EventArguments $e.NewEvent.TargetInstance
} | Out-Null
Register-EngineEvent -SourceIdentifier PowerShellProcess.Created -Action {
param (
[System.Object]$sender
)
$process = Get-Process -Id $sender.ProcessId
$process.ProcessorAffinity = $processorAffinity
} | Out-Null
Main