| 1 |
#!powershell |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2022, John McCall (@lowlydba) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 8 |
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils |
| 9 |
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" } |
| 10 |
|
| 11 |
$ErrorActionPreference = "Stop" |
| 12 |
|
| 13 |
$spec = @{ |
| 14 |
supports_check_mode = $true |
| 15 |
options = @{ |
| 16 |
backup_location = @{type = 'str'; required = $false } |
| 17 |
cleanup_time = @{type = 'int'; required = $false; default = 0 } |
| 18 |
output_file_dir = @{type = 'str'; required = $false } |
| 19 |
replace_existing = @{type = 'bool'; required = $false; } |
| 20 |
log_to_table = @{type = 'bool'; required = $false; default = $false } |
| 21 |
solution = @{type = 'str'; required = $false; choices = @('All', 'Backup', 'IntegrityCheck', 'IndexOptimize'); default = 'All' } |
| 22 |
install_jobs = @{type = 'bool'; required = $false; default = $false } |
| 23 |
local_file = @{type = 'str'; required = $false } |
| 24 |
database = @{type = 'str'; required = $true } |
| 25 |
force = @{type = 'bool'; required = $false; default = $false } |
| 26 |
install_parallel = @{type = 'bool'; required = $false; default = $false } |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec)) |
| 31 |
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module |
| 32 |
$database = $module.Params.database |
| 33 |
$backupLocation = $module.Params.backup_location |
| 34 |
$outputFileDirectory = $module.Params.output_file_dir |
| 35 |
$cleanupTime = $module.Params.cleanup_time |
| 36 |
$replaceExisting = $module.Params.replace_existing |
| 37 |
$solution = $module.Params.solution |
| 38 |
$installJobs = $module.Params.install_jobs |
| 39 |
$installParallel = $module.Params.install_parallel |
| 40 |
$logToTable = $module.Params.log_to_table |
| 41 |
$localFile = $module.Params.local_file |
| 42 |
$force = $module.Params.force |
| 43 |
$checkMode = $module.CheckMode |
| 44 |
$module.Result.changed = $false |
| 45 |
|
| 46 |
try { |
| 47 |
$maintenanceSolutionSplat = @{ |
| 48 |
SqlInstance = $sqlInstance |
| 49 |
SqlCredential = $sqlCredential |
| 50 |
Database = $database |
| 51 |
LogToTable = $logToTable |
| 52 |
Solution = $solution |
| 53 |
InstallJobs = $installJobs |
| 54 |
InstallParallel = $installParallel |
| 55 |
WhatIf = $checkMode |
| 56 |
Force = $force |
| 57 |
Confirm = $false |
| 58 |
EnableException = $true |
| 59 |
} |
| 60 |
if ($null -ne $localFile) { |
| 61 |
$maintenanceSolutionSplat.LocalFile = $localFile |
| 62 |
} |
| 63 |
if ($null -ne $backupLocation) { |
| 64 |
$maintenanceSolutionSplat.BackupLocation = $backupLocation |
| 65 |
} |
| 66 |
if ($null -ne $outputFileDirectory) { |
| 67 |
$maintenanceSolutionSplat.OutputFileDirectory = $outputFileDirectory |
| 68 |
} |
| 69 |
if ($installJobs -eq $true -and $null -ne $cleanupTime) { |
| 70 |
$maintenanceSolutionSplat.CleanupTime = $cleanupTime |
| 71 |
} |
| 72 |
# Only pass if true, otherwise removes warning that is used to track changed=$false |
| 73 |
if ($replaceExisting -eq $true) { |
| 74 |
$maintenanceSolutionSplat.ReplaceExisting = $replaceExisting |
| 75 |
} |
| 76 |
|
| 77 |
try { |
| 78 |
$output = Install-DbaMaintenanceSolution @maintenanceSolutionSplat |
| 79 |
$module.Result.changed = $true |
| 80 |
} |
| 81 |
catch { |
| 82 |
$errMessage = $_.Exception.Message |
| 83 |
if ($errMessage -like "*Maintenance Solution already exists*") { |
| 84 |
$server = Connect-DbaInstance -SqlInstance $sqlInstance -SqlCredential $sqlCredential |
| 85 |
$output = [PSCustomObject]@{ |
| 86 |
ComputerName = $server.ComputerName |
| 87 |
InstanceName = $server.ServiceName |
| 88 |
SqlInstance = $server.DomainInstanceName |
| 89 |
Results = "Success" |
| 90 |
} |
| 91 |
} |
| 92 |
else { |
| 93 |
Write-Error -Message $errMessage |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ($null -ne $output) { |
| 98 |
$resultData = ConvertTo-SerializableObject -InputObject $output |
| 99 |
$module.Result.data = $resultData |
| 100 |
} |
| 101 |
$module.ExitJson() |
| 102 |
} |
| 103 |
catch { |
| 104 |
$module.FailJson("Installing Maintenance Solution failed.", $_) |
| 105 |
} |