问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/23421507/get-service-status-from-remote-server-using-powershell