通过


启用 TCP 协议

注释

SQL Server PowerShell 模块有两种;SqlServer 和 SQLPS 。

SqlServer 模块是当前要使用的 PowerShell 模块。

虽然 SQL Server 安装附带了 SQLPS 模块(用于实现后向兼容性),但该模块不再更新。

SqlServer 模块不仅包含 SQLPS 更新版本的 cmdlet,还包含新的 cmdlet 以支持最新的 SQL 功能 。

PowerShell 库安装 SqlServer 模块。

有关详细信息,请访问 SQL Server PowerShell

使用 SQLPS 连接到控制台时启用 TCP 协议。

  1. 打开命令提示符并键入:

    C:\> SQLPS.EXE
    

    小窍门

    如果未找到 SQLPS,则可能需要打开新的命令提示符,或者只需注销并重新登录。

  2. 在 PowerShell 命令提示符处,键入:

    # Instantiate a ManagedComputer object that exposes primitives to control the
    # Installation of SQL Server on this machine.
    
    $wmi = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' localhost
    
    # Enable the TCP protocol on the default instance. If the instance is named,
    # replace MSSQLSERVER with the instance name in the following line.
    
    $tcp = $wmi.ServerInstances['MSSQLSERVER'].ServerProtocols['Tcp']
    $tcp.IsEnabled = $true
    $tcp.Alter()
    
    # You need to restart SQL Server for the change to persist
    # -Force takes care of any dependent services, like SQL Agent.
    # Note: If the instance is named, replace MSSQLSERVER with MSSQL$ followed by
    # the name of the instance (e.g., MSSQL$MYINSTANCE)
    
    Restart-Service -Name MSSQLSERVER -Force
    

连接到控制台但不使用 SQLPS 时启用 TCP 协议

  1. 打开命令提示符并键入:

    C:\> PowerShell.exe
    
  2. 在 PowerShell 命令提示符处,键入:

    # Get access to SqlWmiManagement DLL on the machine with SQL
    # we are on, which is where SQL Server was installed.
    # Note: This is installed in the GAC by SQL Server Setup.
    
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SqlWmiManagement')
    
    # Instantiate a ManagedComputer object that exposes primitives to control the
    # Installation of SQL Server on this machine.
    
    $wmi = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' localhost
    
    # Enable the TCP protocol on the default instance. If the instance is named,
    # replace MSSQLSERVER with the instance name in the following line.
    
    $tcp = $wmi.ServerInstances['MSSQLSERVER'].ServerProtocols['Tcp']
    $tcp.IsEnabled = $true
    $tcp.Alter()
    
    # You need to restart SQL Server for the change to persist
    # -Force takes care of any dependent services, like SQL Agent.
    # Note: If the instance is named, replace MSSQLSERVER with MSSQL$ followed by
    # the name of the instance (e.g., MSSQL$MYINSTANCE)
    
    Restart-Service -Name MSSQLSERVER -Force