次の方法で共有


Add-SqlAvailabilityDatabase

Adds primary databases to an availability group or joins secondary databases to an availability group.

構文

ByPath (既定)

Add-SqlAvailabilityDatabase
    [[-Path] <String[]>]
    -Database <String[]>
    [-Script]
    [-AccessToken <PSObject>]
    [-TrustServerCertificate]
    [-HostNameInCertificate <String>]
    [-Encrypt <String>]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

ByObject

Add-SqlAvailabilityDatabase
    [-InputObject] <AvailabilityGroup[]>
    -Database <String[]>
    [-Script]
    [-AccessToken <PSObject>]
    [-TrustServerCertificate]
    [-HostNameInCertificate <String>]
    [-Encrypt <String>]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

説明

The Add-SqlAvailabilityDatabase cmdlet adds primary databases to an availability group or joins secondary databases to an availability group. The InputObject or Path parameter specifies the availability group. A database can belong to only one availability group.

To add databases to an availability group, run this cmdlet on the server instance that hosts the primary replica. Specify one or more local user databases.

To join a secondary database to the availability group, manually prepare the secondary database on the server instance that hosts the secondary replica. Then run this cmdlet on the server instance that hosts the secondary replica.

Module requirements: version 21+ on PowerShell 5.1; version 22+ on PowerShell 7.x.

Example 1: Add a database to an availability group

PS C:\> Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16"

This command adds the database 'Database16' to the availability group 'MainAG'. Run this command on the primary server instance of the availability group. This command does not prepare secondary databases for data synchronization.

Example 2: Join a database to an availability group

PS C:\> Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\SecondaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16"

This command joins a secondary database named 'Database16' to the availability group 'MainAG' on one of the server instances that hosts a secondary replica.

Example 3: Add a database and join a secondary database to an availability group

PS C:\> $DatabaseBackupFile = "\\share\backups\Database16.bak"
PS C:\> $LogBackupFile = "\\share\backups\Database16.trn"
PS C:\> $AGPrimaryPath = "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG"
PS C:\> $MyAGSecondaryPath = "SQLSERVER:\SQL\SecondaryServer\InstanceName\AvailabilityGroups\MainAG"
PS C:\> Backup-SqlDatabase -Database "Database16" -BackupFile $DatabaseBackupFile -ServerInstance "PrimaryServer\InstanceName"
PS C:\> Backup-SqlDatabase -Database "Database16" -BackupFile $LogBackupFile -ServerInstance "PrimaryServer\InstanceName" -BackupAction Log
PS C:\> Restore-SqlDatabase -Database "Database16" -BackupFile $DatabaseBackupFile -ServerInstance "SecondaryServer\InstanceName" -NoRecovery
PS C:\> Restore-SqlDatabase -Database "Database16" -BackupFile $LogBackupFile -ServerInstance "SecondaryServer\InstanceName" -RestoreAction Log -NoRecovery
PS C:\> Add-SqlAvailabilityDatabase -Path $AGPrimaryPath -Database 'Database16'
PS C:\> Add-SqlAvailabilityDatabase -Path $AGSecondaryPath -Database "Database16"

This example prepares a secondary database from a database on the server instance that hosts the primary replica of an availability group. It adds the database to an availability group as a primary database.Finally, it joins the secondary database to the availability group.

The sixth command backs up the log file for 'Database16' on the primary server to the location in $LogBackupFile.

Example 4: Create a script to add a database to an availability group

PS C:\> Add-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\PrimaryServer\InstanceName\AvailabilityGroups\MainAG" -Database "Database16" -Script

This command creates a Transact-SQL script that adds the database 'Database16' to the availability group 'MainAG'.

パラメーター

-AccessToken

The access token used to authenticate to SQL Server, as an alternative to user/password or Windows Authentication.

This can be used, for example, to connect to SQL Azure DB and SQL Azure Managed Instance using a Service Principal or a Managed Identity.

The parameter to use can be either a string representing the token or a PSAccessToken object as returned by running Get-AzAccessToken -ResourceUrl https://database.windows.net.

This parameter is new in v22 of the module.

パラメーターのプロパティ

型:PSObject
規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-Confirm

Prompts you for confirmation before running the cmdlet.

パラメーターのプロパティ

型:SwitchParameter
規定値:False
ワイルドカードのサポート:False
DontShow:False
Aliases:cf

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-Database

Specifies an array of user databases. This cmdlet adds or joins the databases that this parameter specifies to the availability group. The databases that you specify must reside on the local instance of SQL Server.

パラメーターのプロパティ

型:

String[]

規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:True
パイプラインからの値:True
プロパティ名別のパイプラインからの値:True
残りの引数からの値:False

-Encrypt

The encryption type to use when connecting to SQL Server.

This value maps to the Encrypt property SqlConnectionEncryptOption on the SqlConnection object of the Microsoft.Data.SqlClient driver.

In v22 of the module, the default is Optional (for compatibility with v21). In v23+ of the module, the default value will be 'Mandatory', which may create a breaking change for existing scripts.

This parameter is new in v22 of the module.

パラメーターのプロパティ

型:String
規定値:None
指定可能な値:Mandatory, Optional, Strict
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-HostNameInCertificate

The host name to be used in validating the SQL Server TLS/SSL certificate. You must pass this parameter if your SQL Server instance is enabled for Force Encryption and you want to connect to an instance using hostname/shortname. If this parameter is omitted then passing the Fully Qualified Domain Name (FQDN) to -ServerInstance is necessary to connect to a SQL Server instance enabled for Force Encryption.

This parameter is new in v22 of the module.

パラメーターのプロパティ

型:String
規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-InputObject

Specifies availability group, as an AvailabilityGroup object, to which this cmdlet adds or joins databases.

パラメーターのプロパティ

型:

AvailabilityGroup[]

規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

ByObject
配置:1
必須:True
パイプラインからの値:True
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-Path

Specifies the path of an availability group to which this cmdlet adds or joins databases. If you do not specify this parameter, this cmdlet uses current working location.

パラメーターのプロパティ

型:

String[]

規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

ByPath
配置:1
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-Script

Indicates that this cmdlet returns a Transact-SQL script that performs the task that this cmdlet performs.

パラメーターのプロパティ

型:SwitchParameter
規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-TrustServerCertificate

Indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.

In v22 of the module, the default is $true (for compatibility with v21). In v23+ of the module, the default value will be '$false', which may create a breaking change for existing scripts.

This parameter is new in v22 of the module.

パラメーターのプロパティ

型:SwitchParameter
規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

パラメーターのプロパティ

型:SwitchParameter
規定値:False
ワイルドカードのサポート:False
DontShow:False
Aliases:wi

パラメーター セット

(All)
配置:Named
必須:False
パイプラインからの値:False
プロパティ名別のパイプラインからの値:False
残りの引数からの値:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

入力

System.String

出力

System.Object