SQL Server 故障转移群集通过 Powershell 更改服务帐户

发布于 2024-12-09 04:34:37 字数 1675 浏览 1 评论 0原文

我创建了下面的 Powershell 脚本来更改 sql server 服务帐户。如果我通过独立的 sql 服务器,这个脚本在我的本地工作正常。但集群 sql 服务失败,并显示以下错误消息:

$OldServiceAccount = '****'
$NewServiceAccount = '*****'
$Password = '****'
$Servers = Get-Content D:\RP-Test\Service_Account_Change\Servers.txt | Select-Object -Unique

foreach($Server in $Servers) {
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null

    $wmi = New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer") $Server
    $Services = $wmi.services | Where-Object { ($_.ServiceAccount -eq $OldServiceAccount) -and ($_.Type -ne 'ReportServer') }

    foreach($Service in $Services) {
        $Service.SetServiceAccount($NewServiceAccount,$Password)
        $Service.Refresh()
    }
    $Services | Select-Object @{ label = "ServiceName"; expression = { $_.Name } }, Type, ServiceAccount, ServiceState | Format-Table -Auto
}

错误消息:

Exception calling "SetServiceAccount" with "2" argument(s): "Set service account failed. "
At line:3 char:27
+ $Service.SetServiceAccount <<<< ($NewServiceAccount,$Password)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我无法远程更改集群 sql 服务帐户。如果我将这个脚本复制到集群的活动节点并在那里执行它,那么这个脚本就可以正常工作。

BOL 表示

对于群集服务器:必须从 SQL Server 群集的活动节点执行更改 SQL Server 或 SQL Server 代理使用的服务帐户。(http://msdn.microsoft.com/en-us/library/ms345578.aspx)

这是否意味着我们需要 TS 到集群的每个活动节点并更改服务帐户。我们不能自动化这个过程吗?这是命中注定吗?或者是否有其他方法可以更改集群 sql 服务的服务帐户。

I have created below Powershell script to change sql server service accounts. This script is working fine from my local if i pass Standalone sql servers. But failing for clustered sql services with below error message:

$OldServiceAccount = '****'
$NewServiceAccount = '*****'
$Password = '****'
$Servers = Get-Content D:\RP-Test\Service_Account_Change\Servers.txt | Select-Object -Unique

foreach($Server in $Servers) {
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null

    $wmi = New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer") $Server
    $Services = $wmi.services | Where-Object { ($_.ServiceAccount -eq $OldServiceAccount) -and ($_.Type -ne 'ReportServer') }

    foreach($Service in $Services) {
        $Service.SetServiceAccount($NewServiceAccount,$Password)
        $Service.Refresh()
    }
    $Services | Select-Object @{ label = "ServiceName"; expression = { $_.Name } }, Type, ServiceAccount, ServiceState | Format-Table -Auto
}

Error Message:

Exception calling "SetServiceAccount" with "2" argument(s): "Set service account failed. "
At line:3 char:27
+ $Service.SetServiceAccount <<<< ($NewServiceAccount,$Password)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I am not able to change the Clustered sql service account remotely. If i copy this script to active node of the cluster and execute it there, then this script isworking fine.

BOL says

For Clustered servers: Changing the service account that is used by SQL Server or SQL Server Agent must be performed from the active node of the SQL Server cluster.(http://msdn.microsoft.com/en-us/library/ms345578.aspx)

Does this mean that we need to TS into each active node of the cluster and change service account. can't we automate this process? is this by desine? or is there any other way of changing service account for the clustered sql services.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文