Advertisement
Guest User

Untitled

a guest
May 9th, 2021
131
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. $ocStartDelaySeconds = 20
  16.  
  17. # process names utilizing gpu that should be ignored, separated by semicolons
  18. # note: miner will be ignored automatically
  19. $processesToIgnore = "mpc-hc64;msedge;csrss;dwm;powershell_ise;chrome;firefox;vlc"
  20.  
  21. ### do not edit anything below this line ###
  22.  
  23. function Start-Mining() {
  24.     Write-Host -ForegroundColor White -BackgroundColor Green "Starting Miner"
  25.     $processId = (Start-Process -FilePath $miner -ArgumentList $minerArguments -NoNewWindow -PassThru).Id
  26.     if($ocStartDelaySeconds -gt 0) {
  27.         Start-Sleep -Seconds $ocStartDelaySeconds
  28.     }
  29.     if($ocSoftwarePath -ne "") {
  30.         Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsMining)"
  31.         Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsMining
  32.     }
  33.  
  34.     return $processId
  35. }
  36.  
  37. function Stop-Mining($processId) {
  38.     Write-Host -ForegroundColor White -BackgroundColor Red "Stopping Miner"
  39.     Stop-Process -Id $processId
  40.     if($ocSoftwarePath -ne "") {
  41.         Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsNormal)"
  42.         Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsNormal
  43.     }
  44. }
  45.  
  46. function Get-GpuProcesses() {
  47.     $processes = @()
  48.     $vcounters = Get-Counter -Counter '\gpu engine(*)\utilization percentage' -MaxSamples 1
  49.     $minerProcessName = (Get-Process -Id $minerProcess).Name
  50.     foreach ($counter in $vcounters) {
  51.         $samples = $counter.CounterSamples
  52.         foreach ($sample in $samples) {
  53.             $cv = $sample.CookedValue
  54.             If ($cv -ne 0) {
  55.                 if($sample.InstanceName.EndsWith("3d") -ne $true) {
  56.                     continue
  57.                 }
  58.  
  59.                 $mpid = $sample.InstanceName.Split("_")[1]
  60.                 $process = Get-Process -Id $mpid
  61.  
  62.  
  63.                 if($process.Name -eq $minerProcessName -or $processesToIgnoreArr.Contains($process.Name)) {
  64.                     continue
  65.                 }
  66.  
  67.  
  68.                 $processes += [pscustomobject]@{
  69.                     Id = $mpid
  70.                     Value = $cv
  71.                     Name = $process.Name
  72.                     Title = if($process.MainWindowTitle -eq "") {$process.Name} else {$process.MainWindowTitle}
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     return $processes
  79. }
  80.  
  81. function Get-GpuUtilization($id) {
  82.     try {
  83.     $val = ((Get-Counter "\GPU Engine(pid_$($id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue)
  84.         if($val.Length -eq 0) {
  85.             return 0
  86.         }
  87.     } catch {
  88.         return 0
  89.     }
  90.     $val = $val[0]
  91.     return $val.CookedValue
  92. }
  93.  
  94. # script starts here
  95. $minerProcess = Start-Mining
  96. $processesToIgnoreArr = $processesToIgnore.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
  97.  
  98. while($true) {
  99.     $gpuProc = Get-GpuProcesses
  100.     foreach($p in $gpuProc) {
  101.         $firstRun = $true
  102.         $gpuUtil = $p.Value
  103.         while($gpuUtil -gt $gpuUtilThreshold) {
  104.             if($firstRun) {
  105.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Detected potential running game: $($p.Title) ($($p.Name))"
  106.             }
  107.  
  108.             Write-Host "$($p.Title): Looping, waiting 15s"
  109.             Start-Sleep -Seconds 15
  110.             $gpuUtil = Get-GpuUtilization($p.Id)
  111.             if($firstRun -and $gpuUtil -lt $gpuUtilThreshold) {
  112.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Temporary spike, not doing anything"
  113.                 break
  114.             } elseif ($firstRun) {
  115.                 $firstRun = $false
  116.                 Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) detected"
  117.                 Stop-Mining($minerProcess)
  118.             } elseif($gpuUtil -gt $gpuUtilThreshold) {
  119.                 Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) still running: $([math]::Round($gpuUtil,2))%"
  120.             }
  121.         }
  122.  
  123.         if ($firstRun -eq $false) {
  124.             Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) ended"
  125.             $minerProcess = Start-Mining
  126.             break
  127.         }
  128.     }
  129.  
  130.     Start-Sleep -Seconds 5
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement