Public/Get-ServiceStatus.ps1
<#
.SYNOPSIS Get the status of a service on a remote computer. .DESCRIPTION This function retrieves the status of a specified service on a remote computer. It checks if the computer is reachable, if the service exists, and then reports its status. .PARAMETER ComputerName Specifies the name of the remote computer to check for the service status. .PARAMETER ServiceName Specifies the name of the service to check the status of on the remote computer. .EXAMPLE Get-ServiceStatus -ComputerName "RemoteComputer" -ServiceName "Spooler" This example retrieves the status of the "Spooler" service on the remote computer "RemoteComputer". .NOTES Requires administrative permissions on the target computers for process monitoring and WMI access. #> function Get-ServiceStatus { param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] [string]$ComputerName, [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)] [string]$ServiceName ) process { if (Test-Connection $ComputerName -Count 2 -Quiet) { try { $ServiceQuery = Get-Service -Name $ServiceName -ComputerName $ComputerName -ErrorAction 'SilentlyContinue' if ($ServiceQuery) { $DisplayName = $ServiceQuery.DisplayName $Status = $ServiceQuery.Status "{0} is {1} on {2}" -f $DisplayName, $Status, $ComputerName } else { "{0} service not found on {1}" -f $ServiceName, $ComputerName } } catch { "{0} - {1}" -f $ComputerName, $_.Exception.Message } } else { "{0} is unreachable" -f $ComputerName } } } |