Guest User

Mining Autoswitch / Game detection

a guest
May 9th, 2021
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # full path to miner
  2. $miner = ""
  3. # arguments for miner
  4. $minerArguments = ""
  5.  
  6. # threshold of gpu utilization where the miner should stop
  7. $gpuUtilThreshold = 15
  8.  
  9. # path to your oc software of choice, leave empty if not applying specific OC
  10. # needs to be scriptable, so far I only know of msi afterburner that allows switching OC profiles
  11. # (arguments for MSI afterburner are -Profile1, -Profile2, etc. it's not possible to 'reset' so make a profile with no OC if desired)
  12. $ocSoftwarePath = "C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe"
  13. $ocArgumentsNormal = "-Profile2"
  14. $ocArgumentsMining = "-Profile1"
  15.  
  16. # process names utilizing gpu that should be ignored, separated by semicolons
  17. # note: miner will be ignored automatically
  18. $processesToIgnore = "mpc-hc64;msedge;csrss;dwm;powershell_ise;chrome;firefox;vlc"
  19.  
  20. ### do not edit anything below this line ###
  21.  
  22. function Start-Mining() {
  23.     if($ocSoftwarePath -ne "") {
  24.         Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsMining)"
  25.         Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsMining
  26.     }
  27.     try {
  28.         Write-Host -ForegroundColor White -BackgroundColor Green "Starting Miner"
  29.         return (Start-Process -FilePath $miner -ArgumentList $minerArguments -NoNewWindow -PassThru).Id
  30.     } catch {
  31.     }
  32. }
  33.  
  34. function Stop-Mining($processId) {
  35.     Write-Host -ForegroundColor White -BackgroundColor Red "Stopping Miner"
  36.     Stop-Process -Id $processId
  37.     if($ocSoftwarePath -ne "") {
  38.         Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsNormal)"
  39.         Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsNormal
  40.     }
  41. }
  42.  
  43. function Get-GpuProcesses() {
  44.     $processes = @()
  45.     $vcounters = Get-Counter -Counter '\gpu engine(*)\utilization percentage' -MaxSamples 1
  46.     $minerProcessName = (Get-Process -Id $minerProcess).Name
  47.     foreach ($counter in $vcounters) {
  48.         $samples = $counter.CounterSamples
  49.         foreach ($sample in $samples) {
  50.             $cv = $sample.CookedValue
  51.             If ($cv -ne 0) {
  52.                 if($sample.InstanceName.EndsWith("3d") -ne $true) {
  53.                     continue
  54.                 }
  55.  
  56.                 $mpid = $sample.InstanceName.Split("_")[1]
  57.                 $process = Get-Process -Id $mpid
  58.  
  59.  
  60.                 if($process.Name -eq $minerProcessName -or $processesToIgnoreArr.Contains($process.Name)) {
  61.                     continue
  62.                 }
  63.  
  64.  
  65.                 $processes += [pscustomobject]@{
  66.                     Id = $mpid
  67.                     Value = $cv
  68.                     Name = $process.Name
  69.                     Title = if($process.MainWindowTitle -eq "") {$process.Name} else {$process.MainWindowTitle}
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     return $processes
  76. }
  77.  
  78. function Get-GpuUtilization($id) {
  79.     try {
  80.     $val = ((Get-Counter "\GPU Engine(pid_$($id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue)
  81.         if($val.Length -eq 0) {
  82.             return 0
  83.         }
  84.     } catch {
  85.         return 0
  86.     }
  87.     $val = $val[0]
  88.     return $val.CookedValue
  89. }
  90.  
  91. # script starts here
  92. $minerProcess = Start-Mining
  93. $processesToIgnoreArr = $processesToIgnore.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
  94.  
  95. while($true) {
  96.     $gpuProc = Get-GpuProcesses
  97.     foreach($p in $gpuProc) {
  98.         $firstRun = $true
  99.         $gpuUtil = $p.Value
  100.         while($gpuUtil -gt $gpuUtilThreshold) {
  101.             if($firstRun) {
  102.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Detected potential running game: $($p.Title) ($($p.Name))"
  103.             }
  104.  
  105.             Write-Host "$($p.Title): Looping, waiting 15s"
  106.             Start-Sleep -Seconds 15
  107.             $gpuUtil = Get-GpuUtilization($p.Id)
  108.             if($firstRun -and $gpuUtil -lt $gpuUtilThreshold) {
  109.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Temporary spike, not doing anything"
  110.                 break
  111.             } elseif ($firstRun) {
  112.                 $firstRun = $false
  113.                 Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) detected"
  114.                 Stop-Mining($minerProcess)
  115.             } elseif($gpuUtil -gt $gpuUtilThreshold) {
  116.                 Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) still running: $([math]::Round($gpuUtil,2))%"
  117.             }
  118.         }
  119.  
  120.         if ($firstRun -eq $false) {
  121.             Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) ended"
  122.             $minerProcess = Start-Mining
  123.             break
  124.         }
  125.     }
  126.  
  127.     Start-Sleep -Seconds 5
  128. }
Advertisement
Add Comment
Please, Sign In to add comment