So with the July Release of SSMS everything changed for using PowerShell with SQL. You can read the details here As I mentioned in my previous post the name of the module has changed to sqlserver
This means that if you have a PowerShell script doing Import-Module SQLPS_, it will need to be changed to be_ Import-Module SqlServer in order to take advantage of the new provider functionality and new CMDLETs. The new module will be installed to “%Program Files\WindowsPowerShell\Modules\SqlServer_” and hence no update to $env:PSModulePath is required._
You can download the latest SSMS release here Once you have installed and rebooted you can start to look at the new Powershell CMDlets
Import-module sqlserver
Take a look at cmdlets
Get-command -module sqlserver
Today I want to look at agent jobs
Get-command *sqlagent*
So I decided to see how to gather the information I gather for the DBADatabase as described here
This is the query I use to insert the data for the server level agent job information.
|
|
So Get-SQLAgentJob looks like the one I need. Lets take a look at the help. This should be the starting point whenever you use a new cmdlet
Get-Help Get-SqlAgentJob -Full
Which states
Returns a SQL Agent Job object for each job that is present in the target instance of SQL Agent.
That sounds like it will meet my needs. Lets take a look
Get-SqlAgentJob -ServerInstance $Connection|ft -AutoSize
I can get the information I require like this
|
|
NOTE – That code is for PowerShell V4 and V5, if you are using earlier versions of PowerShell you would need to use
|
|
But to make the code more performant it is better to do this
|
|
Using Measure-Command showed that this completed in
TotalSeconds : 0.9889336
Rather than
TotalSeconds : 2.9045701
Note that
(Get-SqlAgentJob -ServerInstance $Connection ).where{$_.Enabled -eq $false}.Count
Does not work. I had to check the properties using
|
|
Which showed me
IsEnabled Property bool IsEnabled {get;set;}
So I tested this against the various SQL versions I had in my lab using this code
|
|
Here are the results
I also had a look at Get-SQLAgentJobHistory Lets take a look at the help
Get-help get-SQLAgentJobHistory -showwindow
DESCRIPTION
Returns the JobHistory present in the target instance of SQL Agent.
This cmdlet supports the following modes of operation to return the JobHistory:
- By specifying the Path of the SQL Agent instance.
- By passing the instance of the SQL Agent in the input.
- By invoking the cmdlet in a valid context.
So I ran
Get-SqlAgentJobHistory -ServerInstance sql2014ser12r2
And got back a whole load of information. Every job history available on the server. Too much to look it immediately to work out what to do
So I looked at just one job
Get-SqlAgentJobHistory -ServerInstance SQL2014Ser12R2 -JobName 'DatabaseBackup - SYSTEM_DATABASES - FULL - Local G Drive'
And got back the last months worth of history for that one job as that is the schedule used to purge the job history for this server So then I added -Since Yesterday to only get the last 24 hours history
Get-SqlAgentJobHistory -ServerInstance SQL2014Ser12R2 -JobName 'DatabaseBackup - SYSTEM_DATABASES - FULL - Local G Drive' -Since Yesterday
The Since Parameter is described as
-Since
A convenient abbreviation to avoid using the -StartRunDate parameter.
It can be specified with the -EndRunDate parameter.Do not specify a -StartRunDate parameter, if you want to use it.
Accepted values are:
– Midnight (gets all the job history information generated after midnight)
– Yesterday (gets all the job history information generated in the last 24 hours)
– LastWeek (gets all the job history information generated in the last week)
– LastMonth (gets all the job history information generated in the last month)
When I run
Get-SqlAgentJobHistory -ServerInstance SQL2014Ser12R2 -JobName 'DatabaseBackup - SYSTEM_DATABASES - FULL - Local G Drive' -Since Yesterday |Measure-Object
I get
Count : 3
And if I run
Get-SqlAgentJobHistory -ServerInstance SQL2014Ser12R2 -JobName 'DatabaseBackup - SYSTEM_DATABASES - FULL - Local G Drive' -Since Yesterday |select RunDate,StepID,Server,JobName,StepName,Message|Out-GridView
I get
Which matches the view I see in SSMS Agent Job History
So Get-SqlAgentJobHistory
will enable you to use PowerShell to gather information about the Job history for each step of the Agent Jobs and also the message which I can see being very useful.
Come and join us in the SQL Community Slack to discuss these CMDLets and all things SQL Community https://sqlps.io/slack
CALL TO ACTION
Microsoft are engaging with the community to improve the tools we all use in our day to day work. There is are two Trello boards set up for YOU to use to contribute
https://sqlps.io/vote for SQLPS sqlserver PowerShell module
https://sqlps.io/ssms for SSMS
Go and join them and upvote YOUR preferred choice of the next lot of CMDlets
We have also set up a SQL Community Slack for anyone in the community to discuss all things related to SQL including the Trello board items and already it seems a good place for people to get help with 150+ members in a few days. You can get an invite here https://sqlps.io/slack
Come and join us