SQL 2017 SQLPS執行Add-SqlAvailabilityDatabase異常

SQL Server 2017 SQLPS執行Add-SqlAvailabilityDatabase遇到問題sql

clip_image002


這個錯誤簡直逆天,查了下該cmdlet的幫助shell

clip_image003

常規參數裏有Debug。ide


執行Debug,輸出以下:sqlserver

PS C:\Windows\system32> Add-SqlAvailabilityDatabase -InputObject $ag -Database $DatabaseList -Debugui

調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) In method Proce***ecord. Resolving targets.
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Resolving targets: Parameter Set = 'ByObject'
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Resolved target [App1On]
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) GetShouldProcessTargetString in SqlCmdlet.
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Validating Target
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) ValidateTarget in SqlCmdlet. Returning true.
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Target is valid. Calling BeginTargetProcessing.
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) In BeginTargetProcessing
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Subscribing to server events: InfoMesage, StatementExecuted
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Done with BeginTargetProcessing. Calling ProcessTarget.
詳¨º細?信?息¡é:
select * into #tmpag_availability_groups from master.sys.availability_groups
select agstates.group_id, agstates.primary_replica into #tmpag_availability_group_states from ma
ster.sys.dm_hadr_availability_group_states as agstates
select group_id, replica_id, replica_metadata_id into #tmpag_availability_replicas from master.s
ys.availability_replicas
select replica_id, is_local, role into #tmpag_availability_replica_states from master.sys.dm_had
r_availability_replica_states
SELECT
AG.name AS [Name],
AG.group_id AS [UniqueId],
ISNULL(AG.automated_backup_preference, 4) AS [AutomatedBackupPreference],
ISNULL(AG.failure_condition_level, 6) AS [FailureConditionLevel],
ISNULL(AG.health_check_timeout, -1) AS [HealthCheckTimeout],
ISNULL(agstates.primary_replica, '') AS [PrimaryReplicaServerName],
ISNULL(arstates2.role, 3) AS [LocalReplicaRole],
AR2.replica_metadata_id AS [ID],
ISNULL(AG.basic_features, 0) AS [BasicAvailabilityGroup],
ISNULL(AG.db_failover, 0) AS [DatabaseHealthTrigger],
ISNULL(AG.dtc_support, 0) AS [DtcSupportEnabled],
ISNULL(AG.is_distributed, 1) AS [IsDistributedAvailabilityGroup],
ISNULL(AG.cluster_type, 0) AS [ClusterType],
ISNULL(AG.required_copies_to_commit, 0) AS [RequiredCopiesToCommit]
FROM
#tmpag_availability_groups AS AG
LEFT OUTER JOIN #tmpag_availability_group_states as agstates ON AG.group_id = agstates.group_id
INNER JOIN #tmpag_availability_replicas AS AR2 ON AG.group_id = AR2.group_id
INNER JOIN #tmpag_availability_replica_states AS arstates2 ON AR2.replica_id = arstates2.replica_id AN
D arstates2.is_local = 1
WHERE
(AG.name=@_msparam_0)
drop table #tmpag_availability_groups
drop table #tmpag_availability_group_states
drop table #tmpag_availability_replicas
drop table #tmpag_availability_replica_states
詳¨º細?信?息¡é:
drop table #tmpag_availability_groups
drop table #tmpag_availability_group_states
drop table #tmpag_availability_replicas
drop table #tmpag_availability_replica_states
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Exception occurred Microsoft.SqlServer.Management.Common.ExecutionFa
ilureException: 執¡ä行D Transact-SQL 語®?句?或¨°批¨²處ä|理¤¨ª時º¡À發¤¡é生¦¨²了¢?異°¨¬常¡ê。¡ê ---> System.Data.SqlClient.SqlException: 列¢D名? 'required_co
pies_to_commit' 無T效¡ì。¡ê
在¨² Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Obj
ect execObject, DataSet fillDataSet, Boolean catchException)
在¨² Microsoft.SqlServer.Management.Common.ServerConnection.GetExecuteReader(SqlCommand command)
--- 內¨²部?異°¨¬常¡ê堆?棧?跟¨²蹤Á¨´的Ì?結¨¢尾2 ---
在¨² Microsoft.SqlServer.Management.Common.ServerConnection.GetExecuteReader(SqlCommand command)
在¨² Microsoft.SqlServer.Management.Smo.ExecuteSql.GetDataReader(String query, SqlCommand& command)
在¨² Microsoft.SqlServer.Management.Smo.DataProvider.SetConnectionAndQuery(ExecuteSql execSql, String
query)
在¨² Microsoft.SqlServer.Management.Smo.ExecuteSql.GetDataProvider(StringCollection query, Object con,
StatementBuilder sb, RetriveMode rm)
在¨² Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillData(ResultType resultType, StringCollection
sql, Object connectionInfo, StatementBuilder sb)
在¨² Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillDataWithUseFailure(SqlEnumResult sqlresult,
ResultType resultType)
在¨² Microsoft.SqlServer.Management.Smo.SqlObjectBase.BuildResult(EnumResult result)
在¨² Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData()
在¨² Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req, Object ci)
在¨² Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)
在¨² Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)
在¨² Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetInitDataReader(String[] fields, OrderBy[] orde
rby)
在¨² Microsoft.SqlServer.Management.Smo.SqlSmoObject.ImplInitialize(String[] fields, OrderBy[] orderby
)
在¨² Microsoft.SqlServer.Management.Smo.SqlSmoObject.Initialize(Boolean allProperties)
在¨² Microsoft.SqlServer.Management.Smo.SqlSmoObject.OnPropertyMissing(String propname, Boolean useDef
aultValue)
在¨² Microsoft.SqlServer.Management.Smo.PropertyCollection.RetrieveProperty(Int32 index, Boolean useDe
faultOnMissingValue)
在¨² Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String property
Name, Boolean throwOnNullValue, Boolean useDefaultOnMissingValue)
在¨² Microsoft.SqlServer.Management.Smo.AvailabilityGroup.get_PrimaryReplicaServerName()
在¨² Microsoft.SqlServer.Management.PowerShell.Hadr.CmdletUtilities.IsReplicaPrimary(AvailabilityGroup
ag, SmoRecordContext context)
在¨² Microsoft.SqlServer.Management.PowerShell.Hadr.AddSqlAvailabilityGroupDatabaseCommand.ProcessTarg
et(AvailabilityGroup target, SmoRecordContext context)
在¨² Microsoft.SqlServer.Management.PowerShell.SqlCmdlet`1.Proce***ecord()
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Calling EndProcessing.
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) In EndTargetProcessing
調Ì¡Â試º?: (Add-SqlAvailabilityDatabase) Unsubscribing from server events: InfoMesage, StatementExecuted
Add-SqlAvailabilityDatabase : 執¡ä行D Transact-SQL 語®?句?或¨°批¨²處ä|理¤¨ª時º¡À發¤¡é生¦¨²了¢?異°¨¬常¡ê。¡ê
所¨´在¨²位?置? 行D:1 字Á?符¤?: 1
+ Add-SqlAvailabilityDatabase -InputObject $ag -Database $DatabaseList -Debug
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-SqlAvailabilityDatabase], ExecutionFailureExcep
tion
+ FullyQualifiedErrorId : Microsoft.SqlServer.Management.Common.ExecutionFailureException,Micros
oft.SqlServer.Management.PowerShell.Hadr.AddSqlAvailabilityGroupDatabaseCommand


核心錯誤信息以下:spa

調試: (Add-SqlAvailabilityDatabase) Exception occurred Microsoft.SqlServer.Management.Common.ExecutionFa3d

ilureException: 執行 Transact-SQL 語句或批處理時發生了異常。 ---> System.Data.SqlClient.SqlException: 列名 'required_copies_to_commit' 無效。調試


將相關語句單獨提取出來執行:server

select * into #tmpag_availability_groups
from master.sys.availability_groups
select agstates.group_id, agstates.primary_replica into #tmpag_availability_group_states
from master.sys.dm_hadr_availability_group_states as agstates
select group_id, replica_id, replica_metadata_id into #tmpag_availability_replicas
from master.sys.availability_replicas
select replica_id, is_local, role into #tmpag_availability_replica_states
from master.sys.dm_hadr_availability_replica_states
SELECT
AG.name AS [Name],
AG.group_id AS [UniqueId],
ISNULL(AG.automated_backup_preference, 4) AS [AutomatedBackupPreference],
ISNULL(AG.failure_condition_level, 6) AS [FailureConditionLevel],
ISNULL(AG.health_check_timeout, -1) AS [HealthCheckTimeout],
ISNULL(agstates.primary_replica, '') AS [PrimaryReplicaServerName],
ISNULL(arstates2.role, 3) AS [LocalReplicaRole],
AR2.replica_metadata_id AS [ID],
ISNULL(AG.basic_features, 0) AS [BasicAvailabilityGroup],
ISNULL(AG.db_failover, 0) AS [DatabaseHealthTrigger],
ISNULL(AG.dtc_support, 0) AS [DtcSupportEnabled],
ISNULL(AG.is_distributed, 1) AS [IsDistributedAvailabilityGroup],
ISNULL(AG.cluster_type, 0) AS [ClusterType],
ISNULL(AG.required_copies_to_commit, 0) AS [RequiredCopiesToCommit]
FROM
#tmpag_availability_groups AS AG
LEFT OUTER JOIN #tmpag_availability_group_states as agstates ON AG.group_id = agstates.group_id
INNER JOIN #tmpag_availability_replicas AS AR2 ON AG.group_id = AR2.group_id
INNER JOIN #tmpag_availability_replica_states AS arstates2 ON AR2.replica_id = arstates2.replica_id AND arstates2.is_local = 1
WHERE
(AG.name='App1On')
drop table #tmpag_availability_groups
drop table #tmpag_availability_group_states
drop table #tmpag_availability_replicas
drop table #tmpag_availability_replica_states


得出以下報錯:blog

消息 207,級別 16,狀態 1,第 10 行

列名 'required_copies_to_commit' 無效。


語句中

clip_image004

用到的是

master.sys.availability_groups

中required_copies_to_commit列,而實際上該系統視圖根本沒有該列,只有required_synchronized_secondaries_to_commit列。


查了下網上有一樣的問題:

https://social.msdn.microsoft.com/Forums/azure/en-US/3e5db95a-0231-4e29-b0c0-68c4d8e9583d/exception-occurred-microsoftsqlservermanagementcommonexecutionfailureexception?forum=sqltools

當前運行的SQL Server 2017沒有來得及更新SQLPS。


  • 嘗試打補丁

https://support.microsoft.com/en-us/help/4047329下載最新的累積補丁CU6,安裝SQLServer2017-KB4101464-x64.exe。實際並無對SQLPS模塊進行更新。該問題依然存在。

There are two SQL Server PowerShell modules; SqlServer and SQLPS. The SQLPS module is included with the SQL Server installation (for backwards compatibility), but is no longer being updated. The most up-to-date PowerShell module is the SqlServer module. The SqlServer module contains updated versions of the cmdlets in SQLPS, and also includes new cmdlets to support the latest SQL features.
Previous versions of the SqlServer module were included with SQL Server Management Studio (SSMS), but only with the 16.x versions of SSMS. To use PowerShell with SSMS 17.0 and later, the SqlServer module must be installed from the PowerShell Gallery. To install the SqlServer module, see Install SQL Server PowerShell.


  • 嘗試安裝SqlServer模塊

先下載保存模塊:

Save-Module -Name SqlServer -Path C:\powershellgallery
將下載的SqlServer模塊文件夾拷貝到%ProgramFiles%/WindowsPowershell/Module/下。

加載即用、用時註冊:

Import-Module -Name SqlServer

使用新的SqlServer模塊後,沒有遇到問題了。

相關文章
相關標籤/搜索