Wmi ManagedComputer SetServiceAccount 给出 "Set service account failed"

Wmi ManagedComputer SetServiceAccount gives "Set service account failed"

我正在尝试自动化 post-install 配置 SQL+SSRS 服务器安装(MS SQL 2012 Express)——我认为它或多或少 this。然而,这是我第一次涉足 Powershell,而且我之前从未配置甚至使用过 MS SQL。我的服务器是 Windows 2008 R2 虚拟机,我是管理员,SQL 服务器安装在本地(非集群)。我正在使用 Powershell v2.0 运行 以下代码段(作为管理员):

Write-Output("- Setting service account ...")
Write-Output("  - Loading assembly ...")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
Write-Output("  - Loading server ...")
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer "$hostname"
Write-Output("  - Loading service ...")
#$service = $mc.Services | Where {$_.Name -eq "MSSQL`$$sqlInstanceName"}
$service = $mc.Services | Where {$_.Name -eq "ReportServer`$$sqlInstanceName"}
Write-Output("  - Changing service account credentials ...")
$service.SetServiceAccount("$hostname\sa", "$sapwd")
Write-Output("  - Commit ...")
$service.Alter()
Write-Output("- OK.")

在 "Changing service account credentials" 和 "Commit" 之间,我的代码失败并显示此消息:

Exception calling "SetServiceAccount" with "2" argument(s):
"Set service account failed."

$sapwd 是在 SQL 服务器安装期间提供的密码。我从来没有被要求提供相应的用户名,但我被告知隐含了一个 "SA" 用户。我在本地计算机上没有该名称的 Windows 用户;我不确定在(新安装的)SQL 服务器中定义了哪些用户。

我这里的目标是相当于使用GUI将"Service Account"设置为"Use built-in account: Network Service",代码片段基于this code. This post并非完全无关,但是(a)它没有得到回答,并且(b)我不使用集群。

许多其他人遇到过这个问题,但似乎没有任何可靠的答案对我有用。我如何才能找到有关此错误的含义以及如何解决的信息?

奖金

我还需要更改 "Web Service URL" 和 "Report Manager URL";如果你有任何关于如何做到这一点的提示,你会为我节省很多时间。

sadatabase 用户(内置数据库管理员帐户),而不是 Windows 用户.对于 运行 一项服务,您需要一个 Windows 用户帐户。通常是您为 运行 服务创建的专用帐户(具有所需的 permissions/privileges)或内置默认帐户之一,如 NT AUTHORITY\Network ServiceNT AUTHORITY\Local Service.

使用其中一个内置帐户时,将空字符串作为第二个参数传递,因为它们没有密码:

$service.SetServiceAccount('NT AUTHORITY\Network Service', '')

话虽如此,通常您应该在安装时就指定了数据库服务的runas帐号,以后应该不需要再更改。