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

时间:2018-08-14 11:41:03

标签: sql-server azure powershell

我试图在远程Powershell会话上使用以下命令通过Chocolatey将无人看管的SQL Server 2017安装到Windows 10 Azure虚拟机中:

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)

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

但是,只有在我之前没有连接到具有远程桌面的计算机时,这种情况才会发生,如果我先连接然后启动该过程,它将顺利进行。

问题在于,这必须是一个自动化过程,因此我无法与创建的计算机打开远程桌面连接。

有人对发生的事情有想法吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

在放置赏金后不久,我自己想出了这个办法。 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
}

最后,使用现有的会话对我来说不起作用,我不得不改为将Invoke-Command-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

答案 1 :(得分:0)

在启动SQL Server安装程序之前,在powershell会话中运行此命令:

Enable-WSManCredSSP -Role "Server"