Get service status from remote server using powershell

debal picture debal · May 2, 2014 · Viewed 65.9k times · Source

How to get the service status for a remote computer that needs a user name and password to log in?

I am trying to find a solution using the following code:

$serviceStatus = get-service -ComputerName $machineName -Name $service

The default syntax for the get-service command is:

Parameter Set: Default
Get-Service [[-Name] <String[]> ] [-ComputerName <String[]> ] [-DependentServices] [-Exclude <String[]> ] [-Include <String[]> ] [-RequiredServices] [ <CommonParameters>]

Parameter Set: DisplayName
Get-Service -DisplayName <String[]> [-ComputerName <String[]> ] [-DependentServices] [-Exclude <String[]> ] [-Include <String[]> ] [-RequiredServices] [ <CommonParameters>]

Parameter Set: InputObject
Get-Service [-ComputerName <String[]> ] [-DependentServices] [-Exclude <String[]> ] [-Include <String[]> ] [-InputObject <ServiceController[]> ] [-RequiredServices] [ <CommonParameters>]

This does not have an option for username and password.

Answer

David Brabant picture David Brabant · May 2, 2014

As far as I know, Get-Service doesn't accept a credential parameter. However, you can do it through WMI:

$cred = get-Credential -credential <your domain user here>
Get-WMIObject Win32_Service -computer $computer -credential $cred

Update after comment:

You can save credentials as a securestring into a file, and then reload it for manually creating a credential without having a prompt. See information here.