Unavailable computers missing from get-service command

This topic contains 6 replies, has 5 voices, and was last updated by  Digvijay Nath 3 years, 3 months ago.

  • Author
    Posts
  • #16048

    Digvijay Nath
    Participant

    Hi,
    I am running a query to get the status of service againsts multiple machines.
    The command i am using is –
    Get-Service -name bits -ComputerName s1,s2,s3 | format-table MachineName,Name,Status -AutoSize
    MachineName Name Status
    ———– —- ——
    S1 BITS Stopped
    S3 BITS Stopped

    The output i am getting is only showing the result of the servers that are available.So powershell is omitting the servers on which the command didnt execute fine.
    Is there a way to also list the servers on which the command failed so that i can know on which servers the command didnt work.

    Thanks
    Digvijay

  • #16049

    Adam Bertram
    Moderator

    Try this.

    's1','s2'.'s3' | foreach { Get-Service -name bits -ComputerName $_ | format-table MachineName,Name,Status -AutoSize }

  • #16050

    Richard Siddaway
    Moderator

    I tend to test like this

    $servers = "sphinxdc01", "sphinxdsc01", "sphinxtest"

    $servers |
    foreach {
    if (Test-Connection $psitem -Quiet -Count 1) {
    Get-Service bits -ComputerName $psitem | select MachineName, Name, Status
    }
    else {
    Write-Warning "$psitem can't be reached"
    }

    }

    Replace Write-Warning with whatever code you need

  • #16071

    Boe Prox
    Participant

    Another option:

    $Computername = 's1','s2','s3'
    $Name = 'bits'
    $Computername | ForEach-Object {
    $Computer = $_
    Try {
    Get-Service -name $Name -ComputerName $Computer -ErrorAction Stop
    } Catch {
    $Object = [pscustomobject]@{
    MachineName = $Computer
    Name = $Name
    Status = $_.Exception.Message
    }
    $Object.pstypenames.insert(0,'System.ServiceProcess.ServiceController')
    $Object
    }
    } | Format-Table MachineName, Name, Status -AutoSize

  • #16078

    Digvijay Nath
    Participant

    Thanks Guys. It worked.

  • #16074

    Mike F Robbins
    Participant

    And yet another option:

    
    function Get-MrService {
    
        [CmdletBinding()]
        param (
            [ValidateNotNullOrEmpty()]
            [string[]]$ComputerName = ('s1','s2','s3'),
    
            [ValidateNotNullOrEmpty()]
            [string[]]$ServiceName = 'bits'
        )
    
        foreach ($Computer in $ComputerName) {
    
            foreach ($Service in $ServiceName) {
                
                $ServiceInfo = Get-Service -Name $Service -ComputerName $Computer -ErrorAction SilentlyContinue
    
                if (-not($ServiceInfo)) {
                    $ServiceInfo = @{
                        MachineName = $Computer
                        Name = $Service
                        Status = 'Unreachable'
                    }
                }
    
                [PSCustomObject]@{    
                    ComputerName = $ServiceInfo.MachineName
                    Name = $ServiceInfo.Name
                    Status = $ServiceInfo.Status
                }
            }
        }
    }
    
  • #16075

    Mike F Robbins
    Participant

    I tweaked my code since there's no since in checking for additional services if the first attempt fails.

    
    function Get-MrService {
    
        [CmdletBinding()]
        param (
            [ValidateNotNullOrEmpty()]
            [string[]]$ComputerName = ('s1','s2','s3'),
    
            [ValidateNotNullOrEmpty()]
            [string[]]$ServiceName = 'bits'
        )
    
        foreach ($Computer in $ComputerName) {
            
            $reachable = $true
    
            foreach ($Service in $ServiceName) {
                
                if ($reachable) {
                    $ServiceInfo = Get-Service -Name $Service -ComputerName $Computer -ErrorAction SilentlyContinue
                }
    
                if (-not($ServiceInfo) -or (-not($reachable))) {
                    $ServiceInfo = @{
                        MachineName = $Computer
                        Name = $Service
                        Status = 'Unreachable'
                    }
    
                    $reachable = $false
                }
    
                [PSCustomObject]@{    
                    ComputerName = $ServiceInfo.MachineName
                    Name = $ServiceInfo.Name
                    Status = $ServiceInfo.Status
                }
            }
        }
    }
    

    All of this code is slow though. If PowerShell remoting is enabled on the remote machines, consider talking advantage of it instead of using the ComputerName parameter of Get-Service since using Invoke-Command would cause this to be done in parallel against the remote machines and it would be a lot less likely to be blocked by a firewall.

You must be logged in to reply to this topic.