Get service status from remote server using powershell

前端 未结 4 814
刺人心
刺人心 2021-02-06 01:27

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:

相关标签:
4条回答
  • 2021-02-06 01:56

    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.

    0 讨论(0)
  • 2021-02-06 02:05

    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
    
    0 讨论(0)
  • 2021-02-06 02:13

    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.

    0 讨论(0)
  • 2021-02-06 02: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.

    0 讨论(0)
提交回复
热议问题