commands

^

Install-DbaMaintenanceSolution

Author Viorel Ciucu, cviorel.com
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Install-DbaMaintenanceSolution on GitHub.
Want to see the Bill Of Health for this command? Check out Install-DbaMaintenanceSolution.

Synopsis

Download and Install SQL Server Maintenance Solution created by Ola Hallengren (https://ola.hallengren.com)

Description

This script will download and install the latest version of SQL Server Maintenance Solution created by Ola Hallengren

Syntax

Install-DbaMaintenanceSolution
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String>]
    [[-BackupLocation] <String>]
    [[-CleanupTime] <Int32>]
    [[-OutputFileDirectory] <String>]
    [-ReplaceExisting]
    [-LogToTable]
    [[-Solution] <String[]>]
    [-InstallJobs]
    [[-AutoScheduleJobs] <String[]>]
    [[-StartTime] <String>]
    [[-LocalFile] <String>]
    [-Force]
    [-InstallParallel]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -CleanupTime 72

Installs Ola Hallengren's Solution objects on RES14224 in the DBA database.
Backups will default to the default Backup Directory.
If the Maintenance Solution already exists, the script will be halted.

Example: 2
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -BackupLocation "Z:\SQLBackup" -CleanupTime 72

This will create the Ola Hallengren's Solution objects. Existing objects are not affected in any way.

Example: 3
PS C:\> $params = @{
>> SqlInstance = 'MyServer'
>> Database = 'maintenance'
>> ReplaceExisting = $true
>> InstallJobs = $true
>> LogToTable = $true
>> BackupLocation = 'C:\Data\Backup'
>> CleanupTime = 65
>> Verbose = $true
>> }
>> Install-DbaMaintenanceSolution @params

Installs Maintenance Solution to myserver in database. Adds Agent Jobs, and if any currently exist, they'll be replaced.

Example: 4
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallJobs -BackupLocation "Z:\SQLBackup" -CleanupTime 72 -ReplaceExisting

This will drop and then recreate the Ola Hallengren's Solution objects
The cleanup script will drop and recreate:

  • TABLE [dbo].[CommandLog]
  • STORED PROCEDURE [dbo].[CommandExecute]
  • STORED PROCEDURE [dbo].[DatabaseBackup]
  • STORED PROCEDURE [dbo].[DatabaseIntegrityCheck]
  • STORED PROCEDURE [dbo].[IndexOptimize]
    The following SQL Agent jobs will be deleted:
  • 'Output File Cleanup'
  • 'IndexOptimize - USER_DATABASES'
  • 'sp_delete_backuphistory'
  • 'DatabaseBackup - USER_DATABASES - LOG'
  • 'DatabaseBackup - SYSTEM_DATABASES - FULL'
  • 'DatabaseBackup - USER_DATABASES - FULL'
  • 'sp_purge_jobhistory'
  • 'DatabaseIntegrityCheck - SYSTEM_DATABASES'
  • 'CommandLog Cleanup'
  • 'DatabaseIntegrityCheck - USER_DATABASES'
  • 'DatabaseBackup - USER_DATABASES - DIFF'
Example: 5
PS C:\> Install-DbaMaintenanceSolution -SqlInstance RES14224 -Database DBA -InstallParallel

This will create the Queue and QueueDatabase tables for uses when manually changing jobs to use the @DatabasesInParallel = 'Y' flag

Example: 6
PS C:\> $params = @{
>> SqlInstance = "localhost"
>> InstallJobs = $true
>> CleanupTime = 720
>> AutoSchedule = "WeeklyFull"
>> }
>> Install-DbaMaintenanceSolution @params

This will create the Ola Hallengren's Solution objects and the SQL Agent Jobs.
WeeklyFull will create weekly full, daily differential and 15 minute log backups of user databases.
System databases will be backed up daily.
Databases will be backed up to the default location for the instance, and backups will be deleted after 720 hours (30 days).
See https://github.com/dataplat/dbatools/pull/8911 for details on job schedules.

Example: 7
PS C:\> $params = @{
>> SqlInstance = "localhost"
>> InstallJobs = $true
>> CleanupTime = 720
>> AutoScheduleJobs = "DailyFull", "HourlyLog"
>> BackupLocation = "\\sql\backups"
>> StartTime = "231500"
>> }
PS C:\> Install-DbaMaintenanceSolution @params

This will create the Ola Hallengren's Solution objects and the SQL Agent Jobs.
The jobs will be scheduled to run daily full user backups at 11:15pm, no differential backups will be created and hourly log backups will be made.
System databases will be backed up at 1:15 am, two hours after the user databases.
Databases will be backed up to a fileshare, and the backups will be deleted after 720 hours (30 days).
See https://blog.netnerds.net/2023/05/install-dbamaintenancesolution-now-supports-auto-scheduling/ for more information.

Required Parameters

-SqlInstance

The target SQL Server instance onto which the Maintenance Solution will be installed.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Database

The database where Ola Hallengren's solution will be installed. Defaults to master.

Alias
Required False
Pipeline false
Default Value master
-BackupLocation

Location of the backup root directory. If this is not supplied, the default backup directory will be used.

Alias
Required False
Pipeline false
Default Value
-CleanupTime

Time in hours, after which backup files are deleted.

Alias
Required False
Pipeline false
Default Value 0
-OutputFileDirectory

Specify the output file directory where the Maintenance Solution will write to.

Alias
Required False
Pipeline false
Default Value
-ReplaceExisting

If this switch is enabled, objects already present in the target database will be dropped and recreated.

Alias
Required False
Pipeline false
Default Value False
-LogToTable

If this switch is enabled, the Maintenance Solution will be configured to log commands to a table.

Alias
Required False
Pipeline false
Default Value False
-Solution

Specifies which portion of the Maintenance solution to install. Valid values are All (full solution), Backup, IntegrityCheck and IndexOptimize.

Alias
Required False
Pipeline false
Default Value All
Accepted Values All,Backup,IntegrityCheck,IndexOptimize
-InstallJobs

If this switch is enabled, the corresponding SQL Agent Jobs will be created.

Alias
Required False
Pipeline false
Default Value False
-AutoScheduleJobs

Scheduled jobs during an optimal time. When AutoScheduleJobs is used, this time will be used as the start time for the jobs unless a schedule already
exists in the same time slot. If so, then it will add an hour until it finds an open time slot. Defaults to 1:15 AM.
WeeklyFull will create weekly full, daily differential and 15 minute log backups of user databases.
System databases will always be backed up daily.
Differentials will be skipped when NoDiff or DailyFull is specified.
To perform log backups each hour instead of every 15 minutes, specify HourlyLog in the values.
Recommendations can be found on Ola's site: https://ola.hallengren.com/frequently-asked-questions.html

Alias
Required False
Pipeline false
Default Value
Accepted Values WeeklyFull,DailyFull,NoDiff,FifteenMinuteLog,HourlyLog
-StartTime

When AutoScheduleJobs is used, this time will be used as the start time for the jobs unless a schedule already
exists in the same time slot. If so, then it will add an hour until it finds an open time slot. Defaults to 1:15 AM.

Alias
Required False
Pipeline false
Default Value 011500
-LocalFile

Specifies the path to a local file to install Ola's solution from. This should be the zip file as distributed by the maintainers.
If this parameter is not specified, the latest version will be downloaded and installed from https://github.com/olahallengren/sql-server-maintenance-solution

Alias
Required False
Pipeline false
Default Value
-Force

If this switch is enabled, the Ola's solution will be downloaded from the internet even if previously cached.

Alias
Required False
Pipeline false
Default Value False
-InstallParallel

If this switch is enabled, the Queue and QueueDatabase tables are created, for use when @DatabasesInParallel = 'Y' are set in the jobs.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

Alias cf
Required False
Pipeline false
Default Value