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", "SqlInstance")]
[object]$SqlServer,
[System.Management.Automation.PSCredential]$SqlCredential
)

if ($SqlServer.GetType() -ne [Microsoft.SqlServer.Management.Smo.Server])
{
$SqlServer = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
}

if ($SqlServer.JobServer -eq $null) { return $false }
try { $null = $SqlServer.JobServer.script(); return $true }
catch { return $false }
}