首页 > 解决方案 > PowerShell & SQL Server:尝试为 ManagedComputer 检索对象的数据失败

问题描述

我目前正在编写一个脚本来使用 PowerShell 安装一个新的 SQL Server 实例。
在此脚本中,创建实例后,我禁用动态端口并为其提供一个新的固定端口。

Import-Module SqlPs

[...] # Install a new instance SQL Server

$wmi = (Get-Item "SQLServer:\SQL\$Env:ComputerName").ManagedComputer
$urn = "ManagedComputer[@Name='$Env:ComputerName']/ ServerInstance[@Name='$Instance']/ ServerProtocol[@Name='Tcp']"
$tcp = $wmi.GetSmoObject($urn)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value         = "$Port"
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value = ""
$tcp.Alter()

该脚本工作正常,但是当我尝试在同一个 PowerShell 窗口上第二次运行它时,它返回以下错误:

Exception calling "GetSmoObject" with "1" argument(s): "Attempt to retrieve data for object failed for ManagedComputer 'MYSERVER'."
At .\New-Instance.ps1:316 char:5
+     $tcp = $wmi.GetSmoObject($urn)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FailedOperationException

$Error[0] | Format-List -Force
Exception             : System.Management.Automation.MethodInvocationException: Exception calling "GetSmoObject" with "1" argument(s): "Attempt to retrieve data for object failed for ManagedComputer 'MYSERVER'." --->
                        Microsoft.SqlServer.Management.Smo.FailedOperationException: Attempt to retrieve data for object failed for ManagedComputer 'MYSERVER'. ---> Microsoft.SqlServer.Management.Smo.MissingObjectException: The
                        ServerInstance 'MYINSTANCE' does not exist on the server.
                           at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObjectRec(Urn urn)
                           at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObjectRec(Urn urn)
                           at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObject(Urn urn)
                           --- End of inner exception stack trace ---
                           at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.GetSmoObject(Urn urn)
                           at CallSite.Target(Closure , CallSite , Object , Object )
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          :
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : FailedOperationException
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, D:\SqlServer\Scripts\New-Instance.ps1: line 316
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
PSMessageDetails      :


然而,实例“MYINSTANCE”已成功安装。我认为恢复的 ManagedComputer 尚未更新,并且不包括新实例“MYINSTANCE”。

如何强制刷新恢复的 ManagedComputer ?

我的 PowerShell 版本:

$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  3471

标签: sql-serverpowershellpowershell-5.0

解决方案


推荐阅读