How to Get Service Status From Remote Computer Using PowerShell
This article will discuss connecting to a remote computer, accessing its services, and the service status using PowerShell.
Get Service Status From Remote Computer Using PowerShell
There are many ways to connect to a remote computer and access its services. Windows PowerShell facilitates this by the Get-WmiObject
cmdlet.
It can be used to manage resources on a remote computer easily. You can run the Get-WmiObject
command with a WMI class name to access the local computer services.
This command runs against the local machine by default.
Get-WmiObject Win32_Bus
Output:
The Get-WmiObject
cmdlet provides the -ComputerName
parameter to specify the remote computer. It can be a NetBIOS name or an IP address of the remote machine.
In some scenarios, you might also need to provide a fully qualified domain name. This parameter is not dependent on the Windows PowerShell remoting mechanism.
Hence, it doesn’t rely on the WS-Management protocol. Users do not need to worry about whether their computer is configured to run WM-Management remote commands.
Get-WmiObject -Class Win32_Bus -ComputerName 100.34.35.10
This should connect to the remote computer identified by the 100.34.35.10
IP address and retrieve the Win32_Bus
resource information.
In some scenarios, you need to specify the username and password to connect to the remote host. The Get-WmiObject
cmdlet has a -Credential
parameter to specify the login information to the remote machine.
There are two ways to pass an argument for the -Credential
parameter.
- Provide a username
- Provide a
PsCredential
object
Provide a Username
The Get-WmiObject
cmdlet’s -Credential
parameter accepts just a username or user account name as a text, as shown in the following.
Get-WmiObject Win32_Service -Credential user001 -ComputerName 100.43.10.11
This command will try to connect to the host identified by the 100.43.10.11
IP address. The connection will use the given username (user001
) as the logging user.
This command will prompt the user to enter the password. When the correct password has been provided, it will establish the connection and retrieve the information of the Win32_Service
WMI instance.
Provide a PSCredential
Object
The -Credential
parameter of the Get-WmiObject
cmdlet accepts the PSCredential
object as an argument. This is a very secure way to pass your credentials to any command.
We can easily create a PSCredential
object, as shown below.
$credentialObj = Get-Credential
The Get-Credential
cmdlet returns a PSCredential
object and stores it in the $credentialObj
variable. When you execute the above command in Windows PowerShell, it will prompt a dialog to enter this object’s username and password values.
Let’s enter the username and password as the following.
Username: user01
Password: user01
Let’s check the stored value in the $credentialObj
:
$credentialObj
Output:
The Username
can be viewed as user01
, and the Password
is a PowerShell SecureString
. It is not encouraged to store your passwords in plain text format.
Therefore, the PSCredential
object stores the password as a SecureString
, which is safer.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.