Skip to content

Instantly share code, notes, and snippets.

@hesido
Last active January 20, 2021 18:26
Show Gist options
  • Save hesido/2193eb0ddaa668ea6591161f7b92fb2e to your computer and use it in GitHub Desktop.
Save hesido/2193eb0ddaa668ea6591161f7b92fb2e to your computer and use it in GitHub Desktop.
Powershell script to run batch based on display adapter count
REM Set your powershell script filepath here
set scriptpath="C:\Tools\Devcon\RunBatchBasedOnDisplayAdapterCount.ps1"
timeout 15
REM Run PowerShell in CMD file
REM source:https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-startup-tasks-common
REM Attempt to set the execution policy by using PowerShell version 2.0 syntax.
PowerShell -Version 2.0 -ExecutionPolicy Unrestricted %scriptpath% >> "%TEMP%\StartupLog.txt" 2>&1
REM If PowerShell version 2.0 isn't available. Set the execution policy by using the PowerShell
IF %ERRORLEVEL% EQU -393216 (
PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell %scriptpath% >> "%TEMP%\StartupLog.txt" 2>&1
)
REM If an error occurred, return the errorlevel.
EXIT /B %errorlevel%
#Devcon needs to be installed on the system, you can put it in whatever path you like as long as you define it correctly in the script.
#following lines are self explanatory, but remember that this script counts all display devices,
#so include your on-board display in the count if it is enabled.
#-You can run your favorite miner if number matches
#-Shutdown computer with a delay if the number doesn't match
#-or you may still choose to run the miner with an alternative safe configuration
#-Due to separation of concerns, I have separate batches for notification and they are disabled here.
$numberofdevices = 13
$runwhencountmatch = 'C:\PhoenixMinerLatest\start_phoenix.bat'
$runwhencountmismatch = 'C:\PhoenixMinerLatest\shutdown.bat'
$deviceIdSearchString = '=display'
#Please update the devcon line to match your own system.
$Lines = C:\Tools\Devcon\devcon.exe find $deviceIdSearchString | Select-String "matching"
$Lines -match '(\d+).*'
[int]$GPUCount = [convert]::ToInt32($Matches[1], 10)
if ($GPUCount -eq $numberofdevices)
{
echo ($GPUCount) ' GPUs found. Starting operation.'
#C:\Tools\PushBulletNotifier\Rig-ResetNotification-GPUCountOK.bat
start $runwhencountmatch
}
else
{
echo "GPU count mismatch! Running alternative batch."
#C:\Tools\PushBulletNotifier\Rig-ResetNotification-GPUCountBad.bat
#if this is a batch file that shuts down computer, make sure it gives you plenty of time to escape it when necessary!
start $runwhencountmismatch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment