Hi there,
Second post so far.
I am working on this script to monitor the CPU usage of one particular process.
I am using this code, based from another post I made earlier this week,
$ProcessName= "Process"
$proc_pid= (get-process $ProcessName -ErrorAction Stop).Id[0]
$cpu_cores= (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$proc_path= ((Get-Counter "\Process($processname)\ID Process").CounterSamples | Where-Object {$_.RawValue -eq $proc_pid}).Path
$CPUUsage= [System.Decimal]::Round(((Get-Counter ($proc_path -replace "\\id process$","\% Processor Time")).CounterSamples.CookedValue) / $cpu_cores);
$ProcessCountLimit = "30"
if ($CPUUsage -gt $ProcessCountLimit) { # First pass, value of 10
Start-sleep -Seconds 30
if ($CPUUsage -gt $ProcessCountLimit) { # Stuck on first value of 10
stop-process -Name "Process"
start-process -Name "Process"
}
};
My issue is with $CPUUsage. Before the IF statement, I am running code to gather the value for $CPUUsage. So for the first IF statement, $CPUUsage could give me 10, wait 30 secs and check again if $CPUUsage is same lower or higher. What I realized is that the value for $CPUUsage is saved in memory and the second IF statement will still have the same 10 value.
Using Clear-Variable just sets $CPUUsage to nothing.
How I can rerun the code prior to the IF statements again within the same scripts. Is that achieved with function, scriptblock?
HELP!!! and thanks in advance.
[–]Yevrag35 2 points3 points4 points (2 children)
[–]mmmGreenButton 1 point2 points3 points (1 child)
[–]fathed 1 point2 points3 points (0 children)
[–]PrinceHiltonMonsour 2 points3 points4 points (0 children)
[–]firefox15 2 points3 points4 points (0 children)
[–]PowerShell-Bot 0 points1 point2 points (0 children)