Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlDatabaseRecoveryModel: Database name can now be a pattern #1052

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
The new optional parameters are respectively SqlSvcStartupType, AgtSvcStartupType,
AsSvcStartupType, IsSvcStartupType and RsSvcStartupType ([issue #1165](https://github.com/PowerShell/SqlServerDsc/issues/1165).
[Maxime Daniou (@mdaniou)](https://github.com/mdaniou)
- Changes to SqlDatabaseRecoveryModel
- Now support regex style matching of databases.

## 11.4.0.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDscHelper.psm1') `
-Force
-ChildPath 'SqlServerDscHelper.psm1') `
-Force

$logMessage = "RAISERROR('PowerShell Desired State Configuration Updated database {0} to recovery model {1} because it matched pattern {2}',1,1,1) with log"

<#
.SYNOPSIS
This function gets all Key properties defined in the resource schema file
Expand Down Expand Up @@ -49,22 +52,39 @@ function Get-TargetResource

if ($sqlServerObject)
{
Write-Verbose -Message "Getting RecoveryModel of SQL database '$Name'"
$sqlDatabaseObject = $sqlServerObject.Databases[$Name]
$databases = $sqlServerObject.Databases.Where{$_.Name -like "$Name"}

if ($sqlDatabaseObject)
if(!$databases)
{
$sqlDatabaseRecoveryModel = $sqlDatabaseObject.RecoveryModel
New-VerboseMessage -Message "The current recovery model used by database $Name is '$sqlDatabaseRecoveryModel'"
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
}
else

$sqlDatabaseRecoveryModel = ""
foreach($sqlDatabaseObject in $databases)
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
if($sqlDatabaseObject.Name -eq "tempdb")
{
New-VerboseMessage -Message "Skipping 'tempdb', recovery model for this DB cannot be changed"

Continue
}

New-VerboseMessage -Message "The current recovery model used by database $($sqlDatabaseObject.Name) is '$($sqlDatabaseObject.RecoveryModel)'"

if($sqlDatabaseRecoveryModel -notlike "*$($sqlDatabaseObject.RecoveryModel)*")
{
$sqlDatabaseRecoveryModel += ",$($sqlDatabaseObject.RecoveryModel)"
}
}
}

if([string]::IsNullOrWhiteSpace($sqlDatabaseRecoveryModel) -eq $false)
{
$sqlDatabaseRecoveryModel = $sqlDatabaseRecoveryModel.SubString(1, $sqlDatabaseRecoveryModel.Length - 1)
}

$returnValue = @{
Name = $Name
RecoveryModel = $sqlDatabaseRecoveryModel
Expand Down Expand Up @@ -122,24 +142,46 @@ function Set-TargetResource

if ($sqlServerObject)
{
Write-Verbose -Message "Setting RecoveryModel of SQL database '$Name'"
$sqlDatabaseObject = $sqlServerObject.Databases[$Name]
$databases = $sqlServerObject.Databases.Where{$_.Name -like "$Name"}

if ($sqlDatabaseObject)
if(!$databases)
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
}

foreach($sqlDatabaseObject in $databases)
{
if($sqlDatabaseObject.Name -eq "tempdb")
{
New-VerboseMessage -Message "Skipping 'tempdb', recovery model because this DB cannot be changed"

Continue
}

Write-Verbose -Message "Setting RecoveryModel of SQL database '$($sqlDatabaseObject.Name)'"

if ($sqlDatabaseObject.RecoveryModel -ne $RecoveryModel)
{
$sqlDatabaseObject.RecoveryModel = $RecoveryModel
$sqlDatabaseObject.Alter()
New-VerboseMessage -Message "The recovery model for the database $Name is changed to '$RecoveryModel'."
New-VerboseMessage -Message "The recovery model for the database $($sqlDatabaseObject.Name) is changed to '$RecoveryModel'."

try
{
$null = Invoke-Query -SQLServer $ServerName `
-SQLInstanceName $InstanceName `
-Database $sqlDatabaseObject.Name `
-Query $($logMessage -f $sqlDatabaseObject.Name, $RecoveryModel, $Name) `
-ErrorAction stop
}
catch
{
Write-Warning "Failed to log DSC database recovery model to SQL and event logs."
}
}
}
else
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory InvalidResult
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<#
.EXAMPLE
This example shows how to set the Recovery Model to "Simple" for SQL databases that match the
given pattern.
#>

Configuration Example
{
param
(
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SqlAdministratorCredential
)

Import-DscResource -ModuleName SqlServerDsc

node localhost
{
SqlDatabase Add_SqlDatabaseAdventureworks
{
Ensure = 'Present'
Name = 'Adventureworks'
ServerName = 'sqltest.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SqlAdministratorCredential
}

SqlDatabase Add_SqlDatabaseAdventureWorks2012
{
Ensure = 'Present'
Name = 'AdventureWorks2012'
ServerName = 'sqltest.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SqlAdministratorCredential
}

SqlDatabase Add_SqlDatabaseAdventureWorks2014
{
Ensure = 'Present'
Name = 'AdventureWorks2014'
ServerName = 'sqltest.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SqlAdministratorCredential
}

SqlDatabaseRecoveryModel Set_SqlDatabaseRecoveryModel_AdventureWorks2012
{
Name = 'AdventureWorks201[24]'
RecoveryModel = 'Simple'
ServerName = 'sqltest.company.local'
InstanceName = 'DSC'
PsDscRunAsCredential = $SqlAdministratorCredential
}
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ Read more about recovery model in this article

#### Parameters

* **`[String]` Name** _(Key)_: The SQL database name.
* **`[String]` Name** _(Key)_: The SQL database name or pattern matching
multiple databases.
* **`[String]` ServerName** _(Key)_: The host name of the SQL Server to be configured.
* **`[String]` InstanceName** _(Key)_: The name of the SQL instance to be configured.
* **`[String]` RecoveryModel** _(Required)_: The recovery model to use for the database.
Expand Down
Loading