Script to run a program at a specific CPU load

I have tried for 3 hours now, with several codes similar to this:

wmic cpu get loadpercentage > Load.txt
findstr "%random:~,1%" Load.txt > Load1.txt
set load=<Load1.txt
if %load%==" 2 7                              " echo yes
pause

      

But they all face a similar problem, the conclusion is wmic cpu get loadpercentage

:

LoadPercentage
56

      

The format just doesn't allow you to put it in a variable, so I can't test it. On the contrary, I would like it to be done in Windows CMD and / or Powershell.

Thanks for the help!

EDIT:

Thanks to @lit for the code, here's my final code that works great:

:: To find the "GUID" or the codes for each power plan, run the command in CMD "powercfg -list".
set HighPerformanceMode=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
set PowerSaverMode=a1841308-3541-4fab-bc81-f71556f20b4a

:loop
SETLOCAL ENABLEDELAYEDEXPANSION
SET "load="

FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
    IF "!load!" EQU "" (
        set "load=%%~f"
    )
)

if "%load%" geq "65" (
    ping localhost -n 2 >nul
    if "%load%" geq "65" (
        %systemroot%\System32\powercfg.exe /setactive %HighPerformanceMode%
        )
    ) else (
        if "%load%" lss "25" (
            ping localhost -n 2 >nul
            if "%load%" lss "25" (
                %systemroot%\System32\powercfg.exe /setactive %PowerSaverMode%
                )
            )
)

endlocal
ping localhost -n 3 > nul
goto loop

      

Make sure to change the settings HighPerformanceMode

and PowerSaverMode

to have your computer's power plans. The codes can be found by running powercfg -list

in cmd.

Then I made a separate short script that just has "C:\Load Batch\Load Batch.bat"

in it, but you have to change it where the main script is. Then I used a program called "BAT to EXE converter" and put it in Ghost mode and put the newly created .exe program in my startup folder.

EDIT 2:

I don't believe my question is a duplicate, the related question is how to use cpu and ram for what appears to be just to view it, while my question is to get the load percentage as pure text forms for used in script. I know this is only testing for one processor core, but as one goes up, it is very likely that others have similar loads. I searched this site for code that will strip the "LoadPercentage" text at startup wmic cpu get loadpercentage

because I couldn't set it to a variable that way.

+1


source to share


2 answers


Probably the problem is that the output wmic

is in Unicode. How about switching from PowerShell?

Get-CimInstance -ClassName CIM_Processor | select LoadPercentage

      

I'm not sure about the question of what to run.

A typical rollback of shell programmers cmd

usually looks like:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "X="

FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
    IF "!X!" EQU "" (
        set "X=%%~f"
    )
)

ECHO X is %X%

      



EDIT:

In fact, LoadPercentage will be selected for each core. You probably want the arithmetic mean (mean) of them.

Get-CimInstance -ClassName CIM_Processor |
    Measure-Object -Property LoadPercentage -Average |
    Select Average

      

Doing this in a cmd script involves summing the LoadPercentage values ​​and dividing by their number.

SET /A TOTAL=0
SET /A CORE_COUNT=0

FOR /F "usebackq skip=1" %%t IN (`type NUL ^| wmic /node:"%SERVER_NAME%" cpu get loadpercentage ^| findstr .`) DO (
    IF "%%t" NEQ "" (
        SET /A TOTAL=!TOTAL! + %%t
    )
    SET /A CORE_COUNT=!CORE_COUNT! + 1
)

SET /A AVG_UTILIZATION=%TOTAL% / %CORE_COUNT%

ECHO Number of cores: %CORE_COUNT%
ECHO Total CPU utilisation: %TOTAL%
ECHO Average CPU utilisation: %AVG_UTILIZATION%%%

      

+2


source


While writing to a (temporary) file and reading it with set /p

is a possible and valid way, the usual way to get the output of a command into a variable in cmd

a loop is for /f

:

for /f "" %%a in ('wmic cpu get loadpercentage /value ^|find "="') do set /a "%%a"
echo %loadpercentage%

      

As in the answer lit

, the command find

(or findstr

) is used to convert the Unicode output from wmic

to a "cmd" compatible format.
I use a parameter /value

to get the output format, which is easier to parse, and set /a

to get a numeric value (no need to mess with spaces).

The advantage of using it /value

is that you can easily get more than one parameter from the same command wmic

:

for /f "delims=" %%a in ('"wmic cpu get Caption,CurrentClockSpeed,ExtClock,L2CacheSize /value |findstr = "') do set "_%%a"
set _

      



Including the fully quoted command eliminates the need to escape special characters. Of course, this only works if you don't need the quotes on the command line itself.

(without quoting:

for /f "delims=" %%a in ('wmic cpu get Caption^,CurrentClockSpeed^,ExtClock^,L2CacheSize /value ^|findstr = ') do set "_%%a"

      

)

0


source







All Articles