Skip to content

Instantly share code, notes, and snippets.

@soeminnminn
Created December 31, 2024 14:56
Show Gist options
  • Save soeminnminn/ebc6306a873fdbc38bef3541ead9b254 to your computer and use it in GitHub Desktop.
Save soeminnminn/ebc6306a873fdbc38bef3541ead9b254 to your computer and use it in GitHub Desktop.
battery report for windows 10 and 11.
<?php
function php_exec( $cmd ){
if( function_exists('exec') ){
$output = array();
$return_var = 0;
exec($cmd, $output, $return_var);
return implode( " ", array_values($output) );
}else if( function_exists('shell_exec') ){
ob_start();
$output = shell_exec($cmd);
ob_end_clean();
return $output;
}else if( function_exists('system') ){
$return_var = 0;
return system($cmd, $return_var);
}else if( function_exists('passthru') ){
$return_var = 0;
ob_start();
passthru($cmd, $return_var);
$output = ob_get_contents();
ob_end_clean(); //Use this instead of ob_flush()
return $output;
}else if( function_exists('proc_open') ){
$proc=proc_open($cmd,
array(
array("pipe","r"),
array("pipe","w"),
array("pipe","w")
),
$pipes);
return stream_get_contents($pipes[1]);
}else{
return "@PHP_COMMAND_NOT_SUPPORT";
}
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$cmd = 'powercfg /batteryreport';
$stdres = php_exec($cmd);
if (preg_match('/([A-Z]:[^\.]+\.html)\.$/', $stdres, $matches)) {
$file = $matches[1];
if (file_exists($file)) {
$content= file_get_contents($file);
unlink($file);
echo $content;
die;
}
}
echo $stdres;
} else {
echo 'This is not Windows PC!';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment