sql-server - 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
解决方案
推荐阅读
- apple-watch - 如何在独立的 Apple Watch 应用上跟踪活跃用户
- java - 为什么JFrame没有注册“ESC”被按下?
- python - 输入复数时来自 scipy.sparse.linalg 的 eigsh 函数意外退出
- linux - 如何使用原始套接字将 udp 数据包发送到具有假源 ip 和端口的本地进程
- graph - Neo4j 图形模型未训练
- shell - 关于特定 GNU Makefile 条目的问题
- datagrid - 数据网格板条箱新行
- javascript - Chart.JS 更改文本颜色
- autohotkey - RegWrite 不写入注册表
- swift - SwiftUI:超过 4 个组合的文本视图不能与视图修饰符一起使用