Reputation: 5550
I want to display in a browser the load percentage of a cpu trough php. This is the code I am using:
$command ="C:\\wmic cpu get loadpercentage";
echo shell_exec("$command 2>&1 ; echo $?" );
This is the output:
'C:\wmic' is not recognized as an internal or external command, operable program or batch file.
What am I missing?
Update - 1
Change the code to allow spaces between words:
$command ="C:\\wmic^ cpu^ get^ loadpercentage";
'C:\wmic cpu get loadpercentage' is not recognized as an internal or external command, operable program or batch file.
Now the entire line of code is being read, not only 'C:\wmic'
Upvotes: 9
Views: 53310
Reputation: 39
If you are using windows 11, the feature is disabled by default. Simply type "optional features" in the windows search field, "add an optional feature" will be at the top, click "view features", then search for WMIC, check the box to add the feature. Then make sure that it is added to your system's "Path" as stated above.
Upvotes: 3
Reputation: 103
For windows and javascript add the location of folder wbem to path variable
Upvotes: -1
Reputation: 452
This simple edit to the %PATH%
environment variable, worked for me.
Upvotes: -1
Reputation: 61
set path Windows+Pausebreak
> Advanced System Settings
> Environment Variable
> systme varible
> path
> Edit
: C:\Windows\System32\wbem
or
Go C:\Windows\System32\wbem
> wbemtest
and connect then exit.
Upvotes: 6
Reputation: 136975
You have two problems, both of which we explored in the comments above:
The actual WMIC binary is located at C:\Windows\System32\wbem\WMIC.exe
, not C:\wmic
. That path needs to be used in your PHP command.
You are trying to use Unix-style shell concepts (redirecting STDERR
to STDOUT
, chaining commands with ;
, and using echo
and $?
) on a Windows system.
Simply running the command without all that stuff should work:
echo shell_exec("C:\\Windows\\System32\\wbem\\WMIC.exe cpu get loadpercentage");
Upvotes: 7