Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

New to Scripting. Getting error "cannot index into a null array"

$
0
0

I'm trying to build a script that will stop a process when it's hung at 45% CPU usage or more.  I'm getting the error mentioned in the title.  Could someone please review my script and tell me what I'm doing wrong?  Again, I'm very new to powershell scripting so please be patient with me.  Here is the script:

$ProcessName = "dfileman"

foreach ($proc in (Get-WmiObject  Win32_Processor)){
    if($proc.numberofcores -eq $null){
        $cores++
    }else{
        $cores = $cores + $proc.numberofcores
    }

$cpuusage = [Math]::round(((((Get-Counter "\Process($ProcessName)\% Processor Time" -MaxSamples 2).Countersamples) [0].CookedValue)/$cores),2)

if ($cpuusage -gt 45){stop-process ($ProcessName)}



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>