I have the following code implemented:
$ServiceName = $_GET['Service'];
exec("echo . | powershell.exe -c get-service ".$ServiceName,$OutputArray);
echo "<pre>";
print_r($OutputArray);
echo "</pre>";
My $ServiceName
is assigned through get, but for this example.
$_GET['Service'] = "WinRM"; // Service Name is assigned to WinRM For *Windows Remote Management*
Now.. When I run this script, I get the following:
Array
(
[0] => Get-Service : Cannot find any service with service name 'WinRM'.
[1] => At line:1 char:12
[2] => + get-service <<<< WinRM
[3] => + CategoryInfo : ObjectNotFound: (WinRM:String) [Get-Service], Se
[4] => rviceCommandException
[5] => + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.
[6] => Commands.GetServiceCommand
[7] =>
)
Now, when I open Powershell, and run the following:
get-service WinRM
I get returned with the following output:
PS C:\Users\Administrator> get-service WinRM
Status Name DisplayName
------ ---- -----------
Running WinRM Windows Remote Management (WS-Manag...
This happens on a few other service names such as:
$Array = array(
"DNSServer" => "DNS",
"DHCP Client" => "DHCP",
"Web Server" => "IISADMIN",
"Windows Remote Management" => "WinRM"
);
This fails on DNS, IISADMIN, WinRM
in this example... But again, from PS I get returned with:
PS C:\Users\Administrator> get-service DNS,IISADMIN,WinRM
Status Name DisplayName
------ ---- -----------
Running DNS DNS Server
Running IISADMIN IIS Admin Service
Running WinRM Windows Remote Management (WS-Manag...
My overall question is how to improve the reporting from Powershell to PHP and get accurate results from both ends?
This Does succeed some of the time depending on the service name. For example:
$ServiceName = "DHCP";
-- Which is why this Name is not included in the error debugging.. I get returned with:
Array
(
[0] =>
[1] => Status Name DisplayName
[2] => ------ ---- -----------
[3] => Running DHCP DHCP Client
[4] =>
[5] =>
)
Which is what I am looking for.