Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get service status from remote server using powershell

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.

like image 658
debal Avatar asked May 02 '14 05:05

debal


People also ask

How do I find the service status of a PowerShell remote computer?

Use PowerShell to Check Service Status on a Remote Computer You can use the Get-Service cmdlet to get the status of services not only on the local but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name, or an IP address as a computer name.

What is PowerShell get-service status function?

Summary of Create Get-ServiceStatus Function The purpose of this PowerShell function is to filter the Windows services, so that you can see which are running, and which are 'Stopped'.

How do I list running services in PowerShell?

Using the Get-Service PowerShell cmdlet, you can generate a list of Windows Services running on your Windows 10/8/7 computer. Open an elevated PowerShell console, type Get-Service and hit Enter. You will see a list of all the Services installed on your Windows system.


2 Answers

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.

like image 169
David Brabant Avatar answered Oct 03 '22 22:10

David Brabant


This also works:

net use \\server\c$ $password /USER:$username
$service = Get-Service $serviceName -ComputerName $server

Note that password should not be in a secure string.

like image 28
Morten Holmgaard Avatar answered Oct 03 '22 23:10

Morten Holmgaard