chapter nineteen

19 Creating and working with SQL agent objects

 

In the previous chapter, you understood how to filter and retrieve information about a SQL Server Agent. The responsibility of DBAs for SQL Server Agent is also the creation and alteration of those SQL Server Agent objects. This chapter will provide you with all of the tools to do this easily.

Now that you are learning how useful and powerful dbatools is, you will also want to know how to schedule the running of dbatools scripts with SQL Server Agent Jobs, and we will show you the most stable and robust method that we use to do this.

19.1 SQL Server Agent Job creation

In the last chapter, you used Find-DbaCommand to find all of the dbatools commands that interact with the agent. Let’s search for all of the Commands that you can use to create or alter Agent objects. The PowerShell command Get-Command returns commands you can filter by a module such as dbatools with -Module switch, verb with the -Verb switch and by noun with the -Noun switch. As you can see in Listing 19.1, the verbs that we use to create, alter and remove are New, Set and Remove.

19.2 Creating Categories

19.3 New Schedule

19.4 New Proxy

19.5 Create a New Agent Job

19.6 The Job Step

19.7 Bonus Agent Job Commands

19.8 Start-DbaAgentJob

19.9 Get-DbaRunningJob

19.10 Get-DbaAgentJobHistory

19.11 Hands On Lab