Sql-Server-2008-R2

如何使用 PS 更改 SQL Server 服務帳戶

  • April 22, 2021

SQL Server 在 AD 帳戶 SQLAccount1 下執行。如何使其在另一個帳戶 SQLAccount2 下執行?如何在 Powershell 下做到這一點?

  1. 執行此操作的 GUI 方法是轉到 SQL Server 配置管理器。轉到 SQL Server 的屬性並在“登錄”選項卡上更改帳戶名稱。點擊瀏覽按鈕。

在此處輸入圖像描述

然後輸入您希望 SQL Server 登錄的帳戶。點擊檢查名稱,以確保帳戶存在。然後在選擇使用者或組上點擊確定,然後在 SQL Server 屬性上點擊確定。

在此處輸入圖像描述

比重啟。

2.This blog嘿,腳本專家!部落格,作者Aaron Nelson - SQLvariant展示瞭如何在 PowerShell 中執行此操作。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null

$SMOWmiserver = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') "WIN7NetBook" #Suck in the server you want

#These just act as some queries about the SQL Services on the machine you specified.
$SMOWmiserver.Services | select name, type, ServiceAccount, DisplayName, Properties, StartMode, StartupParameters | Format-Table

#Same information just pivot the data
$SMOWmiserver.Services | select name, type, ServiceAccount, DisplayName, Properties, StartMode, StartupParameters | Format-List

#Specify the "Name" (from the query above) of the one service whose Service Account you want to change.

$ChangeService=$SMOWmiserver.Services | where {$_.name -eq "MSSQLSERVER"} #Make sure this is what you want changed!

#Check which service you have loaded first

$ChangeService

$UName="DomainName\UserName"

$PWord="YourPassword"

$ChangeService.SetServiceAccount($UName, $PWord)

#Now take a look at it afterwards

$ChangeService

引用自:https://dba.stackexchange.com/questions/22006