Advertisement
Guest User

Untitled

a guest
May 10th, 2021
109
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;steamwebhelper;EpicGamesLauncher;EpicWebHelper;Discord;XboxPcApp"
  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 | Sort-Object -Property Value -Descending
  76. }
  77.  
  78. function Get-GpuUtilization($name) {
  79.     try {
  80.     $proc = Get-Process -Name $name -ErrorAction Stop
  81.     $val = ((Get-Counter "\GPU Engine(pid_$($proc.Id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue)
  82.         if($val.Length -eq 0) {
  83.             return 0
  84.         }
  85.     } catch {
  86.         return 0
  87.     }
  88.     $val = $val[0]
  89.     return $val.CookedValue
  90. }
  91.  
  92. # script starts here
  93. $minerProcess = Start-Mining
  94. $processesToIgnoreArr = $processesToIgnore.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
  95.  
  96. while($true) {
  97.     $gpuProc = Get-GpuProcesses
  98.     foreach($p in $gpuProc) {
  99.         $firstRun = $true
  100.         $abortFully = $false
  101.         $abortNextRun = $false
  102.         $gpuUtil = $p.Value
  103.         while($abortFully -eq $false) {
  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.Name)
  111.             if($firstRun -and $gpuUtil -lt $gpuUtilThreshold) {
  112.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Temporary spike, not doing anything"
  113.                 $abortFully = $true
  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.                 $abortNextRun = $false
  120.                 Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) still running: $([math]::Round($gpuUtil,2))%"
  121.             } elseif($abortNextRun) {
  122.                 $abortFully = $true
  123.             } else {
  124.                 Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) potentially shut down"
  125.                 $abortNextRun = $true
  126.             }
  127.         }
  128.  
  129.         if ($firstRun -eq $false) {
  130.             Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) ended"
  131.             $minerProcess = Start-Mining
  132.             break
  133.         }
  134.     }
  135.  
  136.     Start-Sleep -Seconds 5
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement