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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)