How can PHP be used to retrieve a list of currently running services on a Windows Server?
To retrieve a list of currently running services on a Windows Server using PHP, you can utilize the `exec()` function to run a command that retrieves this information from the command line. The `sc query` command can be used to list all services and their statuses on a Windows Server. By parsing the output of this command, you can extract the relevant information and display it in your PHP script.
<?php
// Run the 'sc query' command to retrieve a list of all services
exec('sc query state=all', $output);
// Loop through the output to extract the service names and statuses
foreach ($output as $line) {
if (strpos($line, 'SERVICE_NAME') !== false) {
echo substr($line, 14) . PHP_EOL; // Output the service name
}
if (strpos($line, 'STATE') !== false) {
echo substr($line, 17) . PHP_EOL; // Output the service status
}
}
?>