使用 powershell 从远程服务器获取服务状态

使用 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

get-service 命令的默认语法是:

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.

据我所知,Get-Service 不接受凭据参数.但是,您可以通过 WMI:

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

评论后更新:

您可以将凭据作为安全字符串保存到文件中,然后重新加载它以在没有提示的情况下手动创建凭据.查看信息 此处.

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.