无法获取 ManagedComputer 的 smo 对象

Bri*_*n J 5 sql-server powershell

我一直在使用 MSDN 文章“启用或禁用服务器网络协议 (SQL Server PowerShell)”,但遇到了问题:

$uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息是:

Exception calling "GetSmoObject" with "1" argument(s): "Attempt to retrieve data for object failed for ManagedComputer '<computer_name>'."
At line:1 char:24
+ $Np = $wmi.GetSmoObject <<<< ($uri)
    + CategoryInfo           : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId  : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)

我将替换<computer_name>为我的计算机的名称。我还尝试<computer_name>从我的命令中省略(我正在尝试配置运行脚本的计算机),并且我得到了相同的错误,包括它<computer_name>在错误中显示了正确的内容。我也尝试过从一开始就省略所有内容/,但没有成功。

我尝试过使用cmdpowershell、 和sqlps。我以管理员身份登录,并且我的所有窗口都在顶部显示“管理员”,所以我认为这意味着我正在以管理员身份运行命令。

我在 Windows Server 2012 上运行,安装了 MS Sql Server 2012。

我的长期目标是创建一个实用程序来在新计算机上设置 SQL Server,而无需用户交互。

如何使用 MSDN 文章中的代码行解决我的问题?

Bri*_*n J 0

我在 powershell 脚本的开头添加了这一行:

Import-Module "sqlps" -DisableNamechecking
Run Code Online (Sandbox Code Playgroud)

并返回明确放入<computer_name>,现在它可以工作了。

我认为我收到错误是因为没有执行导入模块,但是当我导入它时,我开始将计算机名称留空,并开始收到 XPath 错误。