Unavailable computers missing from get-service command

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

Try this.

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

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

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

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
            }
        }
    }
}

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.

Thanks Guys. It worked.