SQL Server Cannot get smo object for ManagedComputer

nwwlzxa7  于 2023-08-02  发布在  其他
关注(0)|答案(3)|浏览(77)

I have been using the MSDN article Enable or Disable a Server Network Protocol (SQL Server PowerShell) and am running into issues on the line:

$uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"

The error message I get is:

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

I am replacing <computer_name> with the name of my computer. I have also tried omitting <computer_name> from my command (I am trying to configure the machine from which I am running the script), and the I get the same error, including it showing the correct <computer_name> in the error. I have also tried omitting everything from the first / on, with no luck.

I have tried using cmd , powershell , and sqlps . I am logged in as Administrator, and all of my windows say Administrator at the top, so I take that to mean that I am running the commands as administrator.

I am running on Windows Server 2012, with MS Sql Server 2012 installed.

My long term goal is to create a utility to setup SQL Server on a new machine without the need for user interaction.

How can I resolve my issue with the line of code from the MSDN article?

qnakjoqk

qnakjoqk1#

If you failed like me for SQLExpress (version 12 = 2014):

look at this registry location:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp

the manager shows the value, but I cannot change the values in Win10 - UI issue. C:\Windows\System32\SQLServerManager12.msc

8tntrjer

8tntrjer2#

I included this line at the beginning of the powershell script:

Import-Module "sqlps" -DisableNamechecking

and went back to explicitly putting in the <computer_name> , and now it works.

I think I was getting errors because of not doing the import module, but by the time I imported it, I started leaving the computer name blank, and started getting XPath errors.

lmvvr0a8

lmvvr0a83#

Enable TCP and Named Pipes on a Named SQL Server instance Tested against Server 2022 Express

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")

$smo = 'Microsoft.SqlServer.Management.Smo.'  
$wmi = new-object ($smo + 'Wmi.ManagedComputer').
$SQLInstance = $wmi.ServerInstances.Name

#note the dynamic retrieval of the instance name; obviously, this would not work if you had more than one named instance installed on the machine
$uri = "$($wmi.urn.value)/ServerInstance[@Name=`'$SQLInstance`']/ServerProtocol[@Name='Tcp']"
$Tcp = $wmi.GetSmoObject($uri)
$Tcp.IsEnabled = $true
$Tcp.Alter()
$uri = "$($wmi.urn.value)/ ServerInstance[@Name=`'$SQLInstance`']/ServerProtocol[@Name='Np']"
$Np = $wmi.GetSmoObject($uri)
$Np.IsEnabled = $true
$Np.Alter()

Restart-Service "MSSQL`$$($SQLInstance)"

相关问题