サーバー ネットワーク プロトコルを有効または無効にする方法 (SQL Server PowerShell)

TCP ネットワーク プロトコルおよび名前付きパイプ ネットワーク プロトコルは SQL Server セットアップによってインストールされますが、必ずしも有効になっているとは限りません。ネットワーク プロトコルは、次の PowerShell スクリプトを使用するか、SQL Server 構成マネージャを使用することによって有効または無効にすることができます。プロトコルの変更を有効にするには、SQL Server データベース エンジンを停止してから再起動する必要があります。

PowerShell の全般的な情報については、「SQL Server PowerShell の概要」を参照してください。SQL Server 構成マネージャを使用してプロトコルを管理する方法については、「サーバー ネットワーク プロトコルを有効または無効にする方法 (SQL Server 構成マネージャ)」を参照してください。

SQL Server PowerShell (SQLPS.exe) ユーティリティは、PowerShell セッションを起動し、SQL Server PowerShell プロバイダおよびコマンドレットの読み込みと登録を行います。SQL Server PowerShell の代わりに PowerShell (PowerShell.exe) を実行する場合は、最初に次のステートメントを実行して必要なアセンブリを手動で読み込みます。

# Load the assemblies
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")

プロトコルを有効にするスクリプトを次に示します。プロトコルを無効にするには、IsEnabled プロパティを $false に設定します。

SQL Server PowerShell を使用してサーバー ネットワーク プロトコルを有効にするには

  1. 管理者権限を使用してコマンド プロンプトを開きます。

  2. SQL Server PowerShell を起動するには、コマンド プロンプトで「sqlps.exe」と入力します。

  3. 次のステートメントを実行して TCP プロトコルおよび名前付きパイプ プロトコルの両方を有効にします。<computer_name> を、SQL Server を実行しているコンピュータの名前に置き換えます。名前付きインスタンスを構成する場合は、MSSQLSERVER をインスタンス名に置き換えます。

    $smo = 'Microsoft.SqlServer.Management.Smo.'
    $wmi = new-object ($smo + 'Wmi.ManagedComputer').
    
    # List the object properties, including the instance names.
    $Wmi
    
    # Enable the TCP protocol on the default instance.
    $uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
    $Tcp = $wmi.GetSmoObject($uri)
    $Tcp.IsEnabled = $true
    $Tcp.Alter()
    $Tcp
    
    # Enable the named pipes protocol for the default instance.
    $uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"
    $Np = $wmi.GetSmoObject($uri)
    $Np.IsEnabled = $true
    $Np.Alter()
    $Np
    

ローカル コンピュータのプロトコルを構成するには

  • スクリプトをローカルで実行してローカル コンピュータを構成する場合、SQL Server PowerShell ではローカル コンピュータ名を動的に判断でき、スクリプトの柔軟性が向上します。ローカル コンピュータ名を取得するには、$uri 変数の行の設定を次の行に置き換えます。

    $uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
    

SQL Server PowerShell を使用してデータベース エンジンを再起動するには

  • プロトコルを有効または無効にした後は、データベース エンジンを停止してから再起動して、変更を有効にする必要があります。SQL Server PowerShell で既定のインスタンスを停止してから起動するには、次のステートメントを実行します。名前付きインスタンスを停止してから起動するには、'MSSQLSERVER' を 'MSSQL$<instance_name>' に置き換えます。

    # Get a reference to the ManagedComputer class.
    CD SQLSERVER:\SQL\<computer_name>
    $Wmi = (get-item .).ManagedComputer
    # Get a reference to the default instance of the Database Engine.
    $DfltInstance = $Wmi.Services['MSSQLSERVER']
    # Display the state of the service.
    $DfltInstance
    # Stop the service.
    $DfltInstance.Stop();
    # Wait until the service has time to stop.
    # Refresh the cache.
    $DfltInstance.Refresh(); 
    # Display the state of the service.
    $DfltInstance
    # Start the service again.
    $DfltInstance.Start();
    # Wait until the service has time to start.
    # Refresh the cache and display the state of the service.
    $DfltInstance.Refresh(); $DfltInstance