Listing SQL Server jobs
This recipe illustrates how to list SQL Server jobs using PowerShell.
Getting ready
Do a visual check of the SQL Server jobs in your instance. These should be the jobs you will see after you run the script in this recipe:
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module and create a new SMO Server object, as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
$jobs=$server.JobServer.Jobs $jobs | Select Name, OwnerLoginName, LastRunDate, LastRunOutcome | Sort -Property Name | Format-Table -AutoSize
How it works...
Listing SQL Server jobs is a short, simple task in PowerShell. To list the jobs, you first need to get a handle to the JobServer.Jobs
object:
...