TCP プロトコルを有効にする

SQL Server PowerShell モジュールには SqlServerSQLPS の 2 つがあります。

SqlServer モジュールは、使用する現在の PowerShell モジュールです。

SQLPS モジュールは、(後方互換性のため) SQL Server のインストールに含まれていますが、今後更新されることはありません。

SqlServer モジュールには、SQLPS のコマンドレットの更新バージョンに加え、最新の 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