Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Unavailable computers missing from get-service command

$
0
0

And yet another option:

[pre]
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
            }
        }
    }
}

[/pre]


Viewing all articles
Browse latest Browse all 13067

Trending Articles