Skip to content
johlju edited this page Jun 19, 2020 · 2 revisions

SqlAGDatabase

Parameters

Parameter Attribute DataType Description Allowed Values
DatabaseName Required StringArray[] The name of the database(s) to add to the availability group. This accepts wildcards.
ServerName Key String Hostname of the SQL Server where the primary replica of the availability group lives. If the availability group is not currently on this server, the resource will attempt to connect to the server where the primary replica lives.
InstanceName Key String Name of the SQL instance to be configured.
AvailabilityGroupName Key String The name of the availability group in which to manage the database membership(s).
BackupPath Required String The path used to seed the availability group replicas. This should be a path that is accessible by all of the replicas
Ensure Write String Specifies the membership of the database(s) in the availability group. The options are: Present: The defined database(s) are added to the availability group. All other databases that may be a member of the availability group are ignored. Absent: The defined database(s) are removed from the availability group. All other databases that may be a member of the availability group are ignored. The default is 'Present'. Present, Absent
Force Write Boolean When used with 'Ensure = 'Present'' it ensures the specified database(s) are the only databases that are a member of the specified Availability Group. This parameter is ignored when 'Ensure' is 'Absent'.
MatchDatabaseOwner Write Boolean If set to $true, this ensures the database owner of the database on the primary replica is the owner of the database on all secondary replicas. This requires the database owner is available as a login on all replicas and that the PsDscRunAsCredential has impersonate any login, control server, impersonate login, or control login permissions. If set to $false, the owner of the database will be the PsDscRunAsCredential. The default is '$false'
ReplaceExisting Write Boolean If set to $true, this adds the restore option WITH REPLACE. If set to $false, Existing databases and files will block the restore and throw error. The default is '$false'.
ProcessOnlyOnActiveNode Write Boolean Specifies that the resource will only determine if a change is needed if the target node is the active host of the SQL Server Instance.
IsActiveNode Read Boolean Determines if the current node is actively hosting the SQL Server instance.

Description

The SqlAGDatabase DSC resource is used to add databases or remove databases from a specified availability group.

Requirements

  • Target machine must be running Windows Server 2012 or later.
  • Target machine must be running SQL Server Database Engine 2012 or later.
  • Target machine must be running Windows Management Framework (WMF) 5 or later.

Known issues

All issues are not listed here, see here for all open issues.

Examples

Example 1

This example shows how to ensure that the databases 'DB*' and 'AdventureWorks' are members in the Availability Group 'TestAG'.

In the event this is applied to a Failover Cluster Instance (FCI), the ProcessOnlyOnActiveNode property will tell the Test-TargetResource function to evaluate if any changes are needed if the node is actively hosting the SQL Server Instance.

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

    Import-DscResource -ModuleName 'SqlServerDsc'

    Node $AllNodes.NodeName
    {
        SqlAGDatabase 'AddAGDatabaseMemberships'
        {
            AvailabilityGroupName   = 'TestAG'
            BackupPath              = '\\SQL1\AgInitialize'
            DatabaseName            = 'DB*', 'AdventureWorks'
            InstanceName            = 'MSSQLSERVER'
            ServerName              = $Node.NodeName
            Ensure                  = 'Present'
            ProcessOnlyOnActiveNode = $true

            PsDscRunAsCredential    = $SqlAdministratorCredential
        }
    }
}

Example 2

This example shows how to ensure that the databases 'DB*' and 'AdventureWorks' are not members of the Availability Group 'TestAG'.

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

    Import-DscResource -ModuleName 'SqlServerDsc'

    Node $AllNodes.NodeName
    {
        SqlAGDatabase 'RemoveAGDatabaseMemberships'
        {
            AvailabilityGroupName = 'TestAG'
            BackupPath            = '\\SQL1\AgInitialize'
            DatabaseName          = 'DB*', 'AdventureWorks'
            InstanceName          = 'MSSQLSERVER'
            ServerName            = $Node.NodeName
            Ensure                = 'Absent'

            PsDscRunAsCredential  = $SqlAdministratorCredential
        }
    }
}

Example 3

This example shows how to ensure that the databases 'DB*' and 'AdventureWorks' 'are the only members of the Availability Group 'TestAG'.

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

    Import-DscResource -ModuleName 'SqlServerDsc'

    Node $AllNodes.NodeName
    {
        SqlAGDatabase 'TestAGDatabaseMemberships'
        {
            AvailabilityGroupName = 'TestAG'
            BackupPath            = '\\SQL1\AgInitialize'
            DatabaseName          = 'DB*', 'AdventureWorks'
            InstanceName          = 'MSSQLSERVER'
            ServerName            = $Node.NodeName
            Ensure                = 'Present'
            Force                 = $true

            PsDscRunAsCredential  = $SqlAdministratorCredential
        }
    }
}
Clone this wiki locally