微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerShell&SQL Server:尝试为ManagedComputer

如何解决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 举报,一经查实,本站将立刻删除。