Last active
August 29, 2015 14:19
-
-
Save brianddk/95c4145643b013792152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x=wmic process where name="Wmic.exe" get parentprocessid | |
| y=wmic process where ProcessId="x" list /format:table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # [rights] Copyright Dan B. (brianddk) 2015 https://github.com/brianddk | |
| # [license] Licensed under Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 | |
| # [repo] https://gist.github.com/brianddk/95c4145643b013792152 | |
| # [tips] 18MDTTiqPM8ZEo29Cig1wfGdkLNtvyorW5 | |
| function Squash-Hog | |
| { | |
| [CmdletBinding()] | |
| Param([switch]$Release) | |
| $REAL = 0x100 | |
| $HIGH = 0x80 | |
| $ABOVE = 0x8000 | |
| $NORM = 0x20 | |
| $BELOW = 0x4000 | |
| $IDLE = 0x40 | |
| $numProc = $env:NUMBER_OF_PROCESSORS | |
| if($numProc -gt 1) | |
| { | |
| [Int]$ALLP = [Math]::Pow(2, $numProc) - 1 | |
| $PROC1 = 0x02 | |
| } | |
| if($Release.IsPresent) | |
| { | |
| $wps = Get-WmiObject Win32_Process | |
| foreach($wp in $wps) | |
| { | |
| if ($wp.Priority -lt 8) | |
| { | |
| $wp.SetPriority($NORM) | Out-Null | |
| } | |
| } | |
| $pps = Get-Process | |
| foreach ($pp in $pps) | |
| { | |
| if($pp.ProcessorAffinity -eq $PROC1) | |
| { | |
| $pp.ProcessorAffinity = $ALLP | |
| } | |
| } | |
| } | |
| else | |
| { | |
| $keys = | |
| "% Processor Time", | |
| "% User Time", | |
| "% Privileged Time", | |
| "Page Faults/sec", | |
| "IO Read Operations/sec", | |
| "IO Write Operations/sec", | |
| "IO Data Operations/sec", | |
| "IO Other Operations/sec", | |
| "IO Read Bytes/sec", | |
| "IO Write Bytes/sec", | |
| "IO Data Bytes/sec", | |
| "IO Other Bytes/sec" | |
| $ids = | |
| "ID Process", | |
| "Creating Process ID" | |
| $excludes = | |
| "\\$env:COMPUTERNAME\process(idle)".ToLower(), | |
| "\\$env:COMPUTERNAME\process(_total)".ToLower(), | |
| "\\$env:COMPUTERNAME\process(system)".ToLower(), | |
| "\\$env:COMPUTERNAME\process(smss)".ToLower(), | |
| "\\$env:COMPUTERNAME\process(explorer)".ToLower() | |
| $exGlobs = | |
| "\\$env:COMPUTERNAME\process(chrome".ToLower(), | |
| "\\$env:COMPUTERNAME\process(cmd".ToLower(), | |
| "\\$env:COMPUTERNAME\process(powershell".ToLower(), | |
| "\\$env:COMPUTERNAME\process(powershell_ise".ToLower() | |
| $counters=@() | |
| foreach ($key in ($keys+$ids)) | |
| { | |
| $counters += "\Process(*)\$key" | |
| } | |
| $x = Get-Counter $counters -ErrorAction SilentlyContinue | |
| $wps = Get-WmiObject Win32_Process | |
| foreach($wp in $wps) | |
| { | |
| if ($wp.Handle -eq $pid) | |
| { | |
| $wp.SetPriority($REAL) | Out-Null | |
| } | |
| } | |
| $pps = Get-Process | |
| $h = @{} | |
| foreach ($s in $x.CounterSamples) | |
| { | |
| $p = $s.Path | |
| $key = $p.Substring(0, $p.LastIndexOf("\")) | |
| $val = $p.Substring($p.LastIndexOf("\") + 1) | |
| if (-not $h[$key]) | |
| { | |
| $h[$key] = @{} | |
| } | |
| $h[$key][$val] = $s.CookedValue | |
| if($s.CookedValue -eq $pid -and $val -eq "ID Process") | |
| { | |
| $excludes += $key | |
| } | |
| } | |
| foreach ($k in $h.Keys.GetEnumerator()) | |
| { | |
| foreach ($x in $exGlobs) | |
| { | |
| if ( $k.ToLower().StartsWith("$x#") -or $k.ToLower().StartsWith("$x)") ) | |
| { | |
| $excludes += $k | |
| } | |
| } | |
| } | |
| foreach ($x in $excludes) { $h.Remove($x) } | |
| $depth = 2 | |
| $caps = @{} | |
| foreach ($key in $keys) | |
| { | |
| $caps[$key] = @() | |
| foreach ($val in ($h.values.$key | Sort-Object -Descending)) | |
| { | |
| if($val -eq 0 -or $caps[$key].Count -eq $depth) { break } | |
| $caps[$key] += $val | |
| } | |
| } | |
| $squash_pids = @() | |
| $squash_keys = @() | |
| foreach ($item in $h.GetEnumerator()) | |
| { | |
| foreach($cap in $caps.GetEnumerator()) | |
| { | |
| if($cap.Value -contains $item.Value[$cap.Key]) | |
| { | |
| $squash_pids += $item.Value["ID Process"] | |
| $squash_keys += $item.Key | |
| } | |
| } | |
| } | |
| $squash_pids = $squash_pids | Sort-Object | Get-Unique | |
| $squash_keys = $squash_keys | Sort-Object | Get-Unique | |
| $ea_bef = $ErrorActionPreference | |
| $ErrorActionPreference = "Stop" | |
| foreach ($pp in $pps) | |
| { | |
| if($squash_pids -contains $pp.Id) | |
| { | |
| try | |
| { | |
| $errMsg = "Problem with pid $($pp.Id) '$($pp.ProcessName)'" | |
| $vicMsg = "Squashed pid $($pp.Id) '$($pp.ProcessName)'" | |
| ($pp.ProcessorAffinity = $PROC1) *>&1 | Out-Null | |
| if($?) {Write-Host $vicMsg} | |
| else {Write-Host $errMsg} | |
| } | |
| catch {Write-Host $errMsg} | |
| } | |
| } | |
| foreach ($wp in $wps) | |
| { | |
| if($squash_pids -contains $wp.Handle) | |
| { | |
| try | |
| { | |
| $errMsg = "Problem with pid $($wp.Handle) '$($wp.Name)'" | |
| $vicMsg = "Squashed pid $($wp.Handle) '$($wp.Name)'" | |
| $wp.SetPriority($IDLE) *>&1 | Out-Null | |
| if($?) {Write-Host $vicMsg} | |
| else {Write-Host $errMsg} | |
| } | |
| catch { | |
| Write-Host $errMsg} | |
| } | |
| } | |
| $ErrorActionPreference = $ea_bef | |
| } | |
| $isAdmin = ([Security.Principal.WindowsPrincipal] ` | |
| [Security.Principal.WindowsIdentity]::GetCurrent() ` | |
| ).IsInRole( ` | |
| [Security.Principal.WindowsBuiltInRole] ` | |
| "Administrator") | |
| if (!($isAdmin)){ | |
| Write-Host "Try again as Admin" | |
| } | |
| } | |
| #$block = (Get-Command Squash-Hog).ScriptBlock | |
| #Start-Process Powershell.exe -ArgumentList @("-Command", $block) | |
| Squash-Hog -Release | |
| Squash-Hog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment