Skip to content

Instantly share code, notes, and snippets.

@dlennox24
Last active May 28, 2024 19:46
Show Gist options
  • Save dlennox24/511f09c10a4fc2dbcb8e49d5f521875b to your computer and use it in GitHub Desktop.
Save dlennox24/511f09c10a4fc2dbcb8e49d5f521875b to your computer and use it in GitHub Desktop.
# MUST BE RUN AS AN ADMIN
# this script sets the affinity and priority of a given process
# orginally developed to fix mic audio issues in Discord while using VoiceMeeter
# can be set up to run on system startup via Windows Task Scheduler
# script will attempt a set number of times with a delay inbetween before exiting
$processName = "audiodg"
$priority = "HIGH"
$affinity = 2
$sleepDelay = 5 # time in seconds
$attempts = 12
$totalTime = $attempts*$sleepDelay+$sleepDelay
Write-Host "`nSetting process ``$processName`` to priority ``$priority`` and affinity to ``$affinity``.`n"
while($attempts -gt 0) {
if(Get-Process $processName -ErrorAction silentlycontinue) {
$process = Get-Process $processName
$process.ProcessorAffinity = $affinity
$process.PriorityClass = $priority
Write-Host "$processName is running! Set priority and affinity" -ForegroundColor Green
Write-Host "`nClosing in 5 seconds...`n"
Start-Sleep -s 5
stop-process -Id $PID
}
Write-Host "$processName is not running! Trying again in $sleepDelay seconds. $attempts retry attempts remaining." -ForegroundColor Yellow
$attempts--
Start-Sleep -s $sleepDelay
}
Write-Host "`nAffinity and Pritory not set for ``$processname``" -ForegroundColor Red
Write-Host "Process did not start in $totalTime seconds. Try increasing the delay and/or retry attempts if your system takes longer to start ``$processName```n"
@dlennox24
Copy link
Author

README

* = optional

Set up the foundation

  1. Create a folder for scripts (i.e., C:\startup-scripts) and place this script in it
    • Make sure it has .ps1 as its file type
  2. Launch Task Scheduler
  3. * Create a folder in the Task Scheduler Library (i.e., startup-scripts)
    • This can be added to any folder within the Task Scheduler Library. However, it is best to have some organization.
      image
  4. Right-click on the folder where you would like this Task to live and select Create Task...

Creating the Task

General tab

  1. Name and add a description to the task
  2. Select Run only when user is logged on
  3. Check Run with highest privileges
  4. In the Configure for: dropdown, select Windows 10
    image

Triggers tab

  1. Select New... in the Triggers tab
  2. In the Begin the task: dropdown, select At Log on
  3. Verify Any User is selected in the Settings section and Enabled is checked in the Advanced settings section
  4. Select OK
    image

Actions tab

  1. Select New... in the Actions tab
  2. On the Program/Scripts line, add powershell.exe
  3. On the Add arguments (optional):, add -NoExit -ExecutionPolicy Bypass <pathToPs1Script>
    • <pathToPs1Script> is the location of the script you set in step #1 (i.e., C:\startup-scripts\voicemeeter_audiodg_fix.ps1)
    • WARNING: Be careful with scripts that ask you to use the flag -ExectuionPolicy Bypass. This disables security safeguards, so ensure you trust the author before you run them.
  4. Select OK
    image

Conditions and Settings tabs

  1. The default values for these can be retained
    image
    image

Testing the Script

This script can be tested by restarting the computer or right-clicking on it in the Task Scheduler and selecting Run. A PowerShell prompt should come up and show the script's process. The green audiodg is running! Set priority and affinity message means the script was successful and the dialog will automatically close after 5 seconds.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment