如何为Azure Sql数据库弹性池中的所有数据库设置PITR?

时间:2018-10-18 17:08:21

标签: powershell azure-sql-database powershell-v5.0

我认为我遇到了版本问题,因为我想要的cmdlet似乎仅在AzureRM.Sql模块版本4.7.0-preview中可用。

我想将弹性池中许多数据库的PITR保留策略设置为35天。默认情况下,我的vCore池的保留策略为7天,这还不够。我有数百个数据库,因此需要使用PowerShell设置它们。

如果我得到要用Get-AzureRmSqlElasticPoolDatabase更新的数据库列表,然后尝试运行Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy,则在运行后者时会出现此错误:

import-module : The following error occurred while loading the extended type data file: Error in TypeData "Microsoft.Azure.Commands.Sql.Replication.Model.AzureSqlDatabaseCopyModel": The member DefaultDisplayPropertySet is already present.
Error in TypeData "Microsoft.Azure.Commands.Sql.Replication.Model.AzureReplicationLinkModel": The member DefaultDisplayPropertySet is already present.

At line:1 char:1
+ import-module azurerm.sql
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

我尝试过的事情

我尝试删除该模块并重新导入它。同样的错误。 我尝试导入所需版本的模块,并使用第一个命令获取数据库列表,但随后出现此错误:

Get-AzureRmSqlElasticPoolDatabase : The 'Get-AzureRmSqlElasticPoolDatabase' command was found in the module 'AzureRM.Sql', but the module could not be loaded. For more information, run 'Import-Module AzureRM.Sql'.
At line:1 char:8
+ $dbs = Get-AzureRmSqlElasticPoolDatabase -ElasticPoolName $settings.E ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureRmSqlElasticPoolDatabase:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

如果我使用import-module azurerm.sql导入AzureRm模块,则会出现此错误:

import-module : The following error occurred while loading the extended type data file: Error in TypeData "Microsoft.Azure.Commands.Sql.Replication.Model.AzureSqlDatabaseCopyModel": The member DefaultDisplayPropertySet is already present.
Error in TypeData "Microsoft.Azure.Commands.Sql.Replication.Model.AzureReplicationLinkModel": The member DefaultDisplayPropertySet is already present.

At line:1 char:1
+ import-module azurerm.sql
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

模块

Get-Module AzureRm -ListAvailable | select Name, Version

Name    Version
----    -------
AzureRM 6.10.0

Get-Module AzureRm.Sql -ListAvailable | select Name, Version

Name        Version
----        -------
AzureRM.Sql 4.11.5
AzureRM.Sql 4.7.0
AzureRM.Sql 4.4.0

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17134.228
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.228
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

关于如何使它工作的任何想法?

1 个答案:

答案 0 :(得分:1)

您可以尝试安装4.11.4-preview模块的AzureRM.Sql版,请参考此link,并在Powershell管理员环境中使用Install-Module -Name AzureRM.Sql -RequiredVersion 4.11.4-preview -AllowPrerelease

安装后,无需导入模块,可以直接执行命令。如果要检查模块是否安装成功,请导航至C:\Program Files\WindowsPowerShell\Modules\AzureRM.Sql,您将找到一个4.11.4文件夹。

然后尝试使用示例命令为弹性池中的所有数据库设置PITR,这对我来说效果很好。(您可以执行Get-Help Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy来获取命令的用法)

$dbs = Get-AzureRmSqlElasticPoolDatabase -ResourceGroupName "joywebapp" -ServerName "joydb" -ElasticPoolName "joyelastic"
foreach($db in $dbs){
    Set-AzureRmSqlDatabaseBackupShortTermRetentionPolicy -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName -RetentionDays 35
}

enter image description here