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
  • 8
  • 1
    Couldn't you set a scheduled task in Windows to run a Powershell script $Process = Get-Process MyService $Process.ProcessorAffinity = 4 everytime the service is restarted? Just thinking out loud. That may not work though. – Art.Vandelay05 Oct 30 '15 at 18:52
  • Art.Vandelay05's idea should work. Create a script to change process affinity and create an event trigger in Task Scheduler which runs the script after the service is restarted. – bentek Oct 30 '15 at 20:22
  • Yeah, bentek, that's what I was thinking of: event trigger. That should do what you want XyberICE. I just wasn't sure if your 3rd party service would generate an event ID when it restarts. – Art.Vandelay05 Oct 30 '15 at 21:42
  • @Art.Vandelay05 Thanks! I think this will be the easiest solution for me to implement 'cause I've aleady done each of these steps (separately for other purposes). I'll give it a try on Mon and let you know how it works. – XyberICE Nov 01 '15 at 21:18
  • Sweet! I can't wait to hear back. In task scheduler I have setup AD scripts to run when specific Security Event IDs are generated, e.g., when a new user is created. – Art.Vandelay05 Nov 02 '15 at 01:07

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,030
  • 2
  • 24
  • 35
kekimian
  • 136
  • 2
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
  • 8