internal/Test-SqlAgent.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Function Test-SqlAgent
{
<#
.SYNOPSIS
Internal function. Checks to see if SQL Server Agent is running on a server.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [Alias("ServerInstance", "SqlServer")]
        [object]$SqlInstance,
        [PSCredential]$SqlCredential
    )
    
    if ($SqlInstance.GetType() -ne [Microsoft.SqlServer.Management.Smo.Server])
    {
        $SqlInstance = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
    }
    
    if ($SqlInstance.JobServer -eq $null) { return $false }
    try { $null = $SqlInstance.JobServer.script(); return $true }
    catch { return $false }
}