12

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.

4 Answers 4

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you the only person who knows this? Why is this not the top answer for anyone asking about trying to administer remote machines running Windows?
ArtOfWarfare:: Totally Agreed. Spent a couple of hours researching this. Other methods of getting services on remote machine worked (different protocols?). I'm not entirely clear on how/why this worked. Can someone fill in the details for me? Thx
Wow, I spent like 2 days trying to find a way to authenticate to remote computer in PowerShell, question, how secure is it to authenticate like this?
14

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.

1 Comment

I am looking for something more silent. I wish to set the script up in TaskScheduler, and having a prompt for each server's password every 10 mins is not appropriate. :) Is there some way by which I can have the Domein\Username and password set up in the command itself.
2

Just like Morton's answer, but using New-PSDrive for the share and Remove the share to tidy up.

New-PSDrive -Name tempDriveName –PSProvider FileSystem –Root “\\server\c$” -Credential $Cred
$service = Get-Service -Name $serviceName -ComputerName $MachineName 
Remove-PSDrive -Name tempDriveName

Comments

0

Invoke-Command can accomplish this.

$Cred = Get-Credential -Credential "<your domain user here>"
$ScriptBlock = {
    param($Service)
    Get-Service -Name "$Service"
}
$ServiceStatus = Invoke-Command -ComputerName $MachineName -Credential $Cred -ScriptBlock $ScriptBlock -ArgumentList "$Service"

Note that in order to pass the variable $Service to the Get-Service cmdlet in the script block, you have to declare the script block beforehand and define the $Service variable in the param block. You can then pass the $Service argument into the script block via the -ArgumentList parameter.

Invoke-Command also supports running on multiple servers by supplying a comma delimited list of computers for the -ComputerName parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.