Got now this powershell script and modified it to PRTG needs:
function Get-CPU {
param(
[string] $computer = $env:COMPUTERNAME,
[string] $Proc,
[int] $threshold = 0
)
$ErrorActionPreference = "SilentlyContinue"
# Test connection to computer
if( !(Test-Connection -Destination $computer -Count 1) ){
Write-Host "4: Could not connect to :: $computer"
return
}
# Get all the processes
$processes = Get-WmiObject -ComputerName $computer `
-Class Win32_PerfFormattedData_PerfProc_Process `
-Property Name, PercentProcessorTime
$return= @()
# Build up a return list
foreach( $process in $processes ){
if( $process.PercentProcessorTime -ge $threshold `
-and $process.Name -ne "Idle" `
-and $process.Name -match $Proc `
-and $process.Name -ne "_Total"){
$item = "" | Select Name, CPU
$item.Name = $process.Name
$item.CPU = $process.PercentProcessorTime
$return += $item
$item = $null
}
}
# Sort the return data
$cpuusage = $return | measure-object -Property CPU -Average | fl Average | out-string -s | ? {$_}
$cpuusage = $cpuusage.replace("Average : ","")
$cpuusage = $cpuusage.replace("`n","")
$output = $cpuusage.Substring(0, 4) + ":OK"
return $output
}
Get-CPU
PRTG is reporting for example 14,00% instead 1,4%. When running the script from powershell it reports correctly, where I'm wrong with my script?
Add comment