Get service status from remote server using powershell

天大地大妈咪最大 提交于 2019-12-09 06:07:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!