<#################################################################################################### Created by tehMilcho Source for CPUusage functions https://administrator.de/forum/skript-zum-messen-der-cpu-last-fuer-windows-305733.html ####################################################################################################> #################################################################################################### ############################################ Functions ############################################# #################################################################################################### Function Get-PerformanceCounterNameByID { param([UInt32]$id) $buff = New-Object System.Text.StringBuilder(1024) [UInt32]$bSize = $buff.Capacity Add-Type -MemberDefinition '[DllImport("pdh.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern UInt32 PdhLookupPerfNameByIndex(string szMachineName, uint dwNameIndex, System.Text.StringBuilder szNameBuffer, ref uint pcchNameBufferSize);' -Name PerfCounter -Namespace Util $result = [Util.PerfCounter]::PdhLookupPerfNameByIndex($env:COMPUTERNAME, $id, $buff, [Ref]$bSize) if ($result -eq 0){ $buff.ToString().Substring(0, $bSize-1) }else{ Throw 'Get-PerformanceCounterNameById : Konnte lokalisierten Namen nicht finden. Überprüfen sie die übergebene ID.' } } function Get-PerformanceCounterIdByName { param([Parameter(Mandatory=$true)][string]$Name) $counters = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage' -Name Counter).Counter $index = [array]::IndexOf($counters,$Name) if ($index -ne -1){ return $counters[($index-1)] }else{ throw 'Get-PerformanceCounterIdByName: ID für diesen Namen wurde nicht gefunden' } } #################################################################################################### ############################################ Execution ############################################# #################################################################################################### #################################################################################################### #### 1) Get CPU usages from Processes $root = Get-PerformanceCounterIdByName -Name 'Prozess' $sub = Get-PerformanceCounterIdByName -Name 'Prozessorzeit (%)' $counter = "\$(Get-PerformanceCounterNameById $root)(*)\$(Get-PerformanceCounterNameByID $sub)" $CPUusage = Get-Counter -Counter $counter -EA Ignore | select -Expand CounterSamples | sort CookedValue -Desc # | ft InstanceName,@{n='CPU';e={($_.Cookedvalue/100).toString('P')}} -AutoSize # Filter on Aqua Computer Service and expand value $CPUusage = $CPUusage | Where-Object {$_.InstanceName -eq 'aquacomputerservice'} | Select-Object -ExpandProperty CookedValue #################################################################################################### #### 2) Check if usage is higher then expected and restart the serivce if ($CPUusage -gt 8) { #Restart Service Restart-Service -Name 'Aqua Computer Service' -Force #Post to Eventlog Write-EventLog -LogName 'Application' -Source 'Aqua Computer Service' -EventId 1337 -Message 'AquaComputerService restarted due to high CPU usage' -EntryType Warning } # End Script Exit 0