Patrick R.
Patrick R.

Reputation: 1

PowerShell ProccesorAffinity CPU Designation

I am trying to set a script that, when a service is restarted, the script resets the processor affinity to the settings I want.

The code I have used for other projects has worked in the past, but is now failing.

$Process = Get-Process -Name 'SpaceEngineersDedicated'
$Process.ProcessorAffinity = 254

$Process = Get-Process -Name 'SpaceEngineersDedicated'
$Process.ProcessorAffinity = 255

If I had to guess, this is because this is the first time I have tried to set up such a script on a server with two CPUs. (254,255 was for a computer with only one CPU) The server has 16 cores/threads total.

The goal of this script is to force the service to use all cores, as it only uses one core/thread (Core 0, Node 0) originally. I can do this manually from Task Manager, so I am not sure why it fails.

The error the code spits out says that the property ProcessorAffinity cannot be found on this object.

Upvotes: 0

Views: 1625

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19664

Your Get-Process call is returning multiple processes. In the below syntax, we force these to come back as an array of processes and loop over them to set the property:

@(Get-Process -Name 'SpaceEngineersDedicated') |
    ForEach-Object { $_.ProcessorAffinity = 255 }

You cannot utilize Member Enumeration to set properties if more than one is returned:

## This doesn't work unless .Count = 1
@(Get-Process -Name 'SpaceEngineersDedicated').ProcessorAffinity = 255

Upvotes: 1

Related Questions