none
Get-Counter failing with error : A counter with a negative denominator value was detected

    Question

  • Hi Folks,

    I have a weird situation where I am sampling the CPU usage for small duration and taking an average of the usage using the 'Get-Counter' command. This command seems to work well when tried directly in the powershell prompt. However, when we use the same command in script, the following error throws up, 

    Starting process to consume CPU cycles
    Get-Counter : A counter with a negative denominator value was detected.
    At C:\Monitor.ps1:100 char:29
    +         $sys_cpu_obj = Get-Counter <<<<  '\Processor(_Total)\% Processor Time'
        + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
        + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand
    
    PS C:\> Get-Counter '\Processor(_Total)\% Processor Time'
    
    Timestamp                 CounterSamples
    ---------                 --------------
    2/29/2012 9:56:47 AM      \\vms1\processor(_total)\% processor time :
                              26.2817087701422
    
    
    
    
    PS C:\>
    

    Here is the routine that is being executed, 

    function sample-CPUUsage([int] $iterations=3,[int] $sampling_interval=2){
    	[long] $sum=0
    	# Collect an average of physical memory usage
    	for($i=0; $i -lt $iterations; $i++){
    		$sys_cpu_obj = Get-Counter '\Processor(_Total)\% Processor Time'
    		[long] $cpu_usage=$($sys_cpu_obj.CounterSamples).CookedValue
    		$sum+=$cpu_usage
    		Start-Sleep -Seconds $sampling_interval
    	}
    	[long] $sum=[long] ($sum/$iterations) 
    	return $sum 
    }

    Why am I getting this error when running the same command from the script ?


    -Pranav

    Wednesday, February 29, 2012 6:09 PM

Answers

  • Hi,

    I cannot reproduce your issue either. It seems like that this issue a issue related with your environment.

    Based on the error message "A counter with a negative denominator value was detected", it seems like that $iterations sometimes is treated as a negative number. How about change [long] $sum=[long] ($sum/$iterations)
     as

    [long] $sum=[system.math]::abs([long] ($sum/$iterations))

    Best Regards,

    Yan Li 


    Yan Li

    TechNet Community Support

    Thursday, March 01, 2012 5:36 AM

All replies

  • Your function seems to work on my machine.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 29, 2012 6:15 PM
  • This error might be a bug with powershell. It doesn't always shows up. I am not sure what to do with this...

    -Pranav

    Wednesday, February 29, 2012 7:27 PM
  • Hi,

    I cannot reproduce your issue either. It seems like that this issue a issue related with your environment.

    Based on the error message "A counter with a negative denominator value was detected", it seems like that $iterations sometimes is treated as a negative number. How about change [long] $sum=[long] ($sum/$iterations)
     as

    [long] $sum=[system.math]::abs([long] ($sum/$iterations))

    Best Regards,

    Yan Li 


    Yan Li

    TechNet Community Support

    Thursday, March 01, 2012 5:36 AM
  • Dear Folks,

    I have faced this error in a testing environment on a virtual machine, when the no. of vCPUs on the VM actually exceed the no. of cores on the processor.

    In this case, I have a Dual Core i5 Processor with Hyperthreading enabled. The counter returns a correct value on the Host OS which is a Windows 7 Professional 64 bit.

    However, on test VMs if I have more than 2 CPUs , I get this error. I think the calculation is based more on the physical cores rather than on the logical processors, at least for VMs. 

    Hope this helps.

    Best Regards,

    Shri

    Thursday, February 20, 2014 5:00 AM