简体   繁体   English

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

[英]Get service status from remote server using 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

The default syntax for the get-service command is: 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.这没有用户名和密码选项。

As far as I know, Get-Service doesn't accept a credential parameter .据我所知,Get-Service 不接受凭证参数 However, you can do it through WMI :但是,您可以通过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 .请参阅此处的信息。

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.请注意,密码不应位于安全字符串中。

Invoke-Command can accomplish this. Invoke-Command可以做到这一点。

$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.请注意,为了将变量$Service传递给脚本块中的Get-Service cmdlet,您必须事先声明脚本块并在param块中定义$Service变量。 You can then pass the $Service argument into the script block via the -ArgumentList parameter.然后,您可以通过-ArgumentList参数将$Service参数传递到脚本块中。

Invoke-Command also supports running on multiple servers by supplying a comma delimited list of computers for the -ComputerName parameter.通过为-ComputerName参数提供逗号分隔的计算机列表, Invoke-Command还支持在多个服务器上运行。

Just like Morton's answer, but using New-PSDrive for the share and Remove the share to tidy up.就像 Morton 的回答一样,但使用 New-PSDrive 进行共享并删除共享以进行整理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM