Skip to content

Instantly share code, notes, and snippets.

@nzbart
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/27174a8269341c56b72b to your computer and use it in GitHub Desktop.
Save nzbart/27174a8269341c56b72b to your computer and use it in GitHub Desktop.
Get list of SQL Agent jobs and their corresponding schedules.
#requires -modules sqlps
#requires -version 3
param([string]$ServerToQuery = ([Environment]::MachineName), [PSCredential]$SqlCredential)
$ErrorActionPreference = 'Stop'
pushd
ipmo sqlps -DisableNameChecking
popd
$ssmsAssembly = ls -r 'C:\Program Files (x86)\Microsoft SQL Server' -fi sqlmgmt.dll | sort FullName -Descending | select -First 1 -ExpandProperty FullName
Add-Type -Path $ssmsAssembly
$server = New-Object 'Microsoft.SqlServer.Management.Smo.Server' $ServerToQuery
if($SqlCredential) {
$server.ConnectionContext.LoginSecure = $false
$server.ConnectionContext.Login = $SqlCredential.UserName
$server.ConnectionContext.SecurePassword = $SqlCredential.Password
}
$server.JobServer.Jobs | % {
$job = $_
$schedules = $_.JobSchedules
$jobSchedules = $schedules | % {
$schedule = $_
$jobScheduleData = New-Object Microsoft.SqlServer.Management.SqlMgmt.JobScheduleData -ArgumentList $_
$jobScheduleData.Description
}
New-Object psobject -Property ([ordered]@{ Name = $job.Name; Schedules = $jobSchedules -join ', ' })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment