首页 > 解决方案 > 尝试在无人参与的情况下安装 SQL Server 2017 时出现错误“生成 XML 文档时出错”

问题描述

我正在尝试将 SQL Server 2017 无人值守安装到 Windows 10 Azure 虚拟机中,通过巧克力在远程 powershell 会话上使用以下命令:

choco install sql-server-express -ia ""/IACCEPTSQLSERVERLICENSETERMS /FEATURES=SQLEngine /Q /ACTION=install /INSTANCEID=[INSTANCE_NAME] /INSTANCENAME=SQLCHEMETER /FILESTREAMLEVEL=1 /SECURITYMODE=SQL /SAPWD=[SA_PASSWORD] /UPDATEENABLED=FALSE"" -o -y"

但是安装失败,日志文件只告诉我这个异常已经被抛出:

(01) 2018-08-14 06:42:11 Slp: Inner exceptions are being indented
(01) 2018-08-14 06:42:11 Slp: 
(01) 2018-08-14 06:42:11 Slp: Exception type: Microsoft.SqlServer.Chainer.Infrastructure.ChainerInfrastructureException
(01) 2018-08-14 06:42:11 Slp:     Message: 
(01) 2018-08-14 06:42:11 Slp:         There was an error generating the XML document.
(01) 2018-08-14 06:42:11 Slp:     HResult : 0x84b10001
(01) 2018-08-14 06:42:11 Slp:         FacilityCode : 1201 (4b1)
(01) 2018-08-14 06:42:11 Slp:         ErrorCode : 1 (0001)
(01) 2018-08-14 06:42:11 Slp:     Stack: 
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.DataStoreService.SerializeObject(String rootPath, Object objectToSerialize, Boolean saveToCache)
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.DataStoreService.SerializeObject(Object objectToSerialize)
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.PublicConfigurationBridge.Calculate()
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.InputSettingService.CalculateSettings(IEnumerable`1 settingIds)
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.InputSettingService.CalculateAllSettings(Boolean chainerSettingOnly)
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Chainer.Infrastructure.Action.Execute(String actionId, TextWriter errorStream)
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Setup.Chainer.Workflow.ActionInvocation.<>c__DisplayClasse.<ExecuteActionWithRetryHelper>b__b()
(01) 2018-08-14 06:42:11 Slp:         at Microsoft.SqlServer.Setup.Chainer.Workflow.ActionInvocation.ExecuteActionHelper(ActionWorker workerDelegate)
(01) 2018-08-14 06:42:11 Slp:     Inner exception type: System.InvalidOperationException
(01) 2018-08-14 06:42:11 Slp:         Message: 
(01) 2018-08-14 06:42:11 Slp:                 There was an error generating the XML document.
(01) 2018-08-14 06:42:11 Slp:         HResult : 0x80131509
(01) 2018-08-14 06:42:11 Slp:         Stack: 
(01) 2018-08-14 06:42:11 Slp:                 at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
(01) 2018-08-14 06:42:11 Slp:                 at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
(01) 2018-08-14 06:42:11 Slp:                 at Microsoft.SqlServer.Chainer.Infrastructure.DataStoreService.SerializeObject(String rootPath, Object objectToSerialize, Boolean saveToCache)
(01) 2018-08-14 06:42:11 Slp:         Inner exception type: System.Security.Cryptography.CryptographicException
(01) 2018-08-14 06:42:11 Slp:             Message: 
(01) 2018-08-14 06:42:11 Slp:                         Access is denied.
(01) 2018-08-14 06:42:11 Slp:                         
(01) 2018-08-14 06:42:11 Slp:             HResult : 0x80070005
(01) 2018-08-14 06:42:11 Slp:             Stack: 
(01) 2018-08-14 06:42:11 Slp:                         at System.Security.Cryptography.ProtectedData.Protect(Byte[] userData, Byte[] optionalEntropy, DataProtectionScope scope)
(01) 2018-08-14 06:42:11 Slp:                         at Microsoft.SqlServer.Common.SqlSecureString.WriteXml(XmlWriter writer)
(01) 2018-08-14 06:42:11 Slp:                         at System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
(01) 2018-08-14 06:42:11 Slp:                         at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAgentConfigurationPublic.Write6_AgentConfigurationPublic(String n, String ns, AgentConfigurationPublic o, Boolean isNullable, Boolean needType)
(01) 2018-08-14 06:42:11 Slp:                         at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAgentConfigurationPublic.Write7_AgentConfigurationPublic(Object o)

我已经检查过,启动它的用户具有管理员权限。

但这只发生在我之前没有使用远程桌面连接到机器的情况下,如果我连接然后启动该过程它将顺利进行。

问题是这必须是一个自动化的过程,所以我无法打开与创建的机器的远程桌面连接。

有人知道发生了什么吗?

谢谢。

标签: sql-serverazurepowershell

解决方案


发放赏金后不久,我自己就想出了这一点。CredSSP 可以做到这一点,但它不像单线那么简单。基本上,这是“双跳”问题,如果您没有通过身份验证,您将遇到此错误。

要启用 CredSSP,您需要在服务器上运行它(您尝试安装 SQL Server 的远程主机:

Enable-WSManCredSSP -Role Server -Force

您还需要在客户端(委托给服务器的机器)上启用 CredSSP:

Enable-WSManCredSSP -Role client -DelegateComputer $nameOrIp -Force

此外,您需要允许客户端上的凭据委派,这可以在 PowerShell 中使用此脚本自动完成:

$allowed = @("WSMAN/$nameOrIp")
$key = 'hklm:\SOFTWARE\Policies\Microsoft\Windows\CredentialsDelegation'
if (!(Test-Path $key)) {
    md $key
}
New-ItemProperty -Path $key -Name AllowFreshCredentials -Value 1 -PropertyType Dword -Force            
$key = Join-Path $key 'AllowFreshCredentials'
if (!(Test-Path $key)) {
    md $key
}
$i = 1
$allowed |% {
    New-ItemProperty -Path $key -Name $i -Value $_ -PropertyType String -Force
    $i++
}

如果您还没有,请确保两台机器都已将彼此设置为受信任的主机:

$curList = (Get-Item WSMan:\localhost\Client\TrustedHosts).value
$exists = $curList -match $nameOrIp
if ($exists -eq $false) {
  "Adding IP to trusted hosts..."
  Set-Item WSMan:\localhost\Client\TrustedHosts -Value "$curList, $nameOrIp" -Force
}

最后,使用现有的 Session 对我不起作用,我不得不发送Invoke-Commandwith -Authentication CredSSP

$cred = New-Object System.Management.Automation.PSCredential ($adminName, $secureWindowsPassword)
Invoke-Command -Computername $nameOrIp -Authentication CredSSP -Credential $cred -ScriptBlock {
    Param($sqlPass)
    Start-Process -FilePath C:\temp\SQLEXPR_x64_ENU.exe -Args "/ConfigurationFile=C:\temp\MSSQL.ini /SAPWD='$sqlPass' /Q /HIDECONSOLE /UpdateEnabled=FALSE" -Verb RunAs -Wait 4>&1
} -ArgumentList $sqlPass

推荐阅读