From d404d79d3c208821c705d5b2908d318d6aa16184 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Thu, 24 Mar 2022 16:28:30 -0400 Subject: [PATCH] added Get-MemoryUsage and Stop-MemoryHog --- Tools.ps1 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 821860d..efed624 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -650,8 +650,26 @@ function Create-LocalUser { } Else { LogMsg "Cannot create or modify a local account without administrative privileges" } - ## TODO: Hide account from logon screen - #Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" + If ( $Hide ) { + # TODO: Hide account from logon screen + #Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" + } +} + +# Returns the number of MB a process is consuming +function Get-MemoryUsage ([string]$AppName) { + [math]::Round((Get-Process -Name $AppName -ErrorAction SilentlyContinue | Measure-Object -Property WorkingSet -Sum).Sum / 1MB) +} + +# Kills a process if it's using more than the specified amount of memory +function Stop-MemoryHog ([string]$AppName,[int]$MemoryLimit) { + $MemoryUsed = (Get-MemoryUsage -AppName $AppName) + If ( $MemoryUsed -gt $MemoryLimit ) { + $MemoryDifference = $MemoryUsed - $MemoryLimit + LogMsg "Killing ""${AppName}"" for using ${MemoryDifference}MB over the limit of ${MemoryLimit}MB" + Try { Get-Process -Name $AppName -ErrorAction SilentlyContinue | Stop-Process -Force } + Catch { LogErr $_.Exception.Message } + } } Setup-MSPDirectories