3

I'd like to set the Affinity and Priority of a service process when it is started (without modifying the service/process code, I don't own the code, it's a 3rd party service).

I know I can do:

$Process = Get-Process MyService $Process.ProcessorAffinity = 4

in PowerShell to change the Affinity of the process or prefix the command path with:

cmd.exe /c start "Some Process Name" /affinity 4

However, neither of these are applicable to changing the Affinity and Priority of the service's process automatically everytime the service is started) How can I do this without using some 3rd-party app that 'maintains' process affinity across restart (I'd really like to write/create a solution myself and not rely yet another app to 'configure' windows).

XyberICE
  • 141
  • 1
  • 8

2 Answers2

2

Have a look at this post : http://waynes-world-it.blogspot.ca/2009/06/processor-affinity-on-windows-server.html

If you’re trying to control a service, you could use instsrv/srvany to create a service that wraps the start or psexec command around the real service binary. For example, the commands below create another version of the spooler service that will only run on the first processor.

instsrv Test c:\util\srvany.exe 
reg add hklm\system\currentcontrolset\services\test\Parameters
reg add hklm\system\currentcontrolset\services\test\Parameters /v Application /t reg_sz /d cmd.exe 
reg add hklm\system\currentcontrolset\services\test\Parameters /v AppParameters /t reg_sz /d "/c start /affinity 1 C:\WINDOWS\system32\spoolsv.exe"
GregL
  • 9,870
kekimian
  • 136
1

Ok, I built on @Art.Vandelay05's comment and here's the (IMHO most intuitive) solution.

  1. Create the Script.ps1 PowerShell script with the following lines:

    $Process = Get-Process < service_process_exe_name_WITHOUT_the_exe >
    $Process.ProcessorAffinity = < hex version of bit-code for processor affinity >
  2. Go to Administrative Tools/Task Scheduler and Create Task... (NOT Create Basic Task)
  3. General Tab
    • Check Run whether user is logged on or not
    • Change User or Group to a local user on your machine
  4. Triggers
    • New...
    • Enabled
    • Begin the task On an event
    • Edit Event Filter
    • Use a little XSLT to find the message for your service starting. I used:

<QueryList> <Query Id="0" Path="System"> <Select Path="System"> *[EventData[Data[@Name="param1"] and (Data="< your_service_name > ")]] and *[EventData[Data[@Name="param2"] and (Data="running")]] </Select> </Query> </QueryList>

  1. Actions
    • Action Start a program
    • Program/script: PowerShell.exe
    • Add arguments (optional): -File "path_to_script\Script.ps1"

That's it! Now every time your service runs, the affinity will be set according to the PowerShell script you wrote.

Edit: Make sure your local user has the Debug Programs right (Administrative Tools/Local Security Policy/User Rights Assignment/Debug programs/ add your local user to this right) or your user won't be able to change a process's affinity.

XyberICE
  • 141
  • 1
  • 8