Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # full path to miner
- $miner = ""
- # arguments for miner
- $minerArguments = ""
- # threshold of gpu utilization where the miner should stop
- $gpuUtilThreshold = 15
- # path to your oc software of choice, leave empty if not applying specific OC
- # needs to be scriptable, so far I only know of msi afterburner that allows switching OC profiles
- # (arguments for MSI afterburner are -Profile1, -Profile2, etc. it's not possible to 'reset' so make a profile with no OC if desired)
- $ocSoftwarePath = "C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe"
- $ocArgumentsNormal = "-Profile2"
- $ocArgumentsMining = "-Profile1"
- # process names utilizing gpu that should be ignored, separated by semicolons
- # note: miner will be ignored automatically
- $processesToIgnore = "mpc-hc64;msedge;csrss;dwm;powershell_ise;chrome;firefox;vlc;steamwebhelper;EpicGamesLauncher;EpicWebHelper;Discord;XboxPcApp"
- ### do not edit anything below this line ###
- function Start-Mining() {
- if($ocSoftwarePath -ne "") {
- Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsMining)"
- Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsMining
- }
- try {
- Write-Host -ForegroundColor White -BackgroundColor Green "Starting Miner"
- return (Start-Process -FilePath $miner -ArgumentList $minerArguments -NoNewWindow -PassThru).Id
- } catch {
- }
- }
- function Stop-Mining($processId) {
- Write-Host -ForegroundColor White -BackgroundColor Red "Stopping Miner"
- Stop-Process -Id $processId
- if($ocSoftwarePath -ne "") {
- Write-Host -ForegroundColor White -BackgroundColor Green "Applying OC $($ocArgumentsNormal)"
- Start-Process -FilePath $ocSoftwarePath -ArgumentList $ocArgumentsNormal
- }
- }
- function Get-GpuProcesses() {
- $processes = @()
- $vcounters = Get-Counter -Counter '\gpu engine(*)\utilization percentage' -MaxSamples 1
- $minerProcessName = (Get-Process -Id $minerProcess).Name
- foreach ($counter in $vcounters) {
- $samples = $counter.CounterSamples
- foreach ($sample in $samples) {
- $cv = $sample.CookedValue
- If ($cv -ne 0) {
- if($sample.InstanceName.EndsWith("3d") -ne $true) {
- continue
- }
- $mpid = $sample.InstanceName.Split("_")[1]
- $process = Get-Process -Id $mpid
- if($process.Name -eq $minerProcessName -or $processesToIgnoreArr.Contains($process.Name)) {
- continue
- }
- $processes += [pscustomobject]@{
- Id = $mpid
- Value = $cv
- Name = $process.Name
- Title = if($process.MainWindowTitle -eq "") {$process.Name} else {$process.MainWindowTitle}
- }
- }
- }
- }
- return $processes | Sort-Object -Property Value -Descending
- }
- function Get-GpuUtilization($name) {
- try {
- $proc = Get-Process -Name $name -ErrorAction Stop
- $val = ((Get-Counter "\GPU Engine(pid_$($proc.Id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue)
- if($val.Length -eq 0) {
- return 0
- }
- } catch {
- return 0
- }
- $val = $val[0]
- return $val.CookedValue
- }
- # script starts here
- $minerProcess = Start-Mining
- $processesToIgnoreArr = $processesToIgnore.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
- while($true) {
- $gpuProc = Get-GpuProcesses
- foreach($p in $gpuProc) {
- $firstRun = $true
- $abortFully = $false
- $abortNextRun = $false
- $gpuUtil = $p.Value
- while($abortFully -eq $false) {
- if($firstRun) {
- Write-Host -ForegroundColor Black -BackgroundColor Yellow "Detected potential running game: $($p.Title) ($($p.Name))"
- }
- Write-Host "$($p.Title): Looping, waiting 15s"
- Start-Sleep -Seconds 15
- $gpuUtil = Get-GpuUtilization($p.Name)
- if($firstRun -and $gpuUtil -lt $gpuUtilThreshold) {
- Write-Host -ForegroundColor Black -BackgroundColor Yellow "Temporary spike, not doing anything"
- $abortFully = $true
- } elseif ($firstRun) {
- $firstRun = $false
- Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) detected"
- Stop-Mining($minerProcess)
- } elseif($gpuUtil -gt $gpuUtilThreshold) {
- $abortNextRun = $false
- Write-Host -ForegroundColor White -BackgroundColor DarkGreen "Game $($p.Title) ($($p.Name)) still running: $([math]::Round($gpuUtil,2))%"
- } elseif($abortNextRun) {
- $abortFully = $true
- } else {
- Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) potentially shut down"
- $abortNextRun = $true
- }
- }
- if ($firstRun -eq $false) {
- Write-Host -ForegroundColor Black -BackgroundColor Yellow "Game $($p.Title) ($($p.Name)) ended"
- $minerProcess = Start-Mining
- break
- }
- }
- Start-Sleep -Seconds 5
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement