Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Listing SQL Server Jobs


This recipe illustrates how to list SQL Server Jobs using PowerShell.

Getting ready

Do a visual check of your SQL Server Jobs in your instance. You should see the following jobs after you run the script in this recipe:

How to do it...

These are the steps to list SQL Server Jobs:

  1. Open PowerShell ISE as administrator.

  2. Import the SQLPS module and create a new SMO Server object:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName

    Add the following script and run:

    $jobs=$server.JobServer.Jobs
    $jobs |
    Select-Object Name, OwnerLoginName,
    LastRunDate, LastRunOutcome |
    Sort-Object -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:

$jobs=$server.JobServer.Jobs

Once...