
応答ファイルを使用して SQL Server 2016 を無人インストールしました.INI
。応答ファイルでは、次のように TCP を有効にします。
; Specify 0 to disable or 1 to enable the TCP/IP protocol.
TCPENABLED="1"
インストールが完了したら、PowerShell スクリプトを使用してエイリアスも作成します。ここまでは順調です。ただし、SQL Server インスタンスは動的ポートを有効にしてインストールされており、応答ファイルで静的 TCP ポート (標準の 1433 を使用) を指定する方法がわかりません。そのため、エイリアスは機能しません。
応答ファイルまたは PowerShell を使用して静的 TCP ポートを設定するにはどうすればよいでしょうか?
答え1
実行可能なソリューションを長時間探した結果、特定の SQL Server インスタンスの TCP ポートを設定できる次の PowerShell 関数を見つけました。
function SetPort($instance, $port)
{
# fetch the WMI object that contains TCP settings; filter for the 'IPAll' setting only
# note that the 'ComputerManagement13' corresponds to SQL Server 2016
$settings = Get-WmiObject `
-Namespace root/Microsoft/SqlServer/ComputerManagement13 `
-Class ServerNetworkProtocolProperty `
-Filter "InstanceName='$instance' and IPAddressName='IPAll' and PropertyType=1 and ProtocolName='Tcp'"
# there are two settings in a list: TcpPort and TcpDynamicPorts
foreach ($setting in $settings)
{
if ($setting -ne $null)
{
# set the static TCP port and at the same time clear any dynamic ports
if ($setting.PropertyName -eq "TcpPort")
{
$setting.SetStringValue($port)
}
elseif ($setting.PropertyName -eq "TcpDynamicPorts")
{
$setting.SetStringValue("")
}
}
}
}
この関数は次のように使用されます。
SetPort "SQLInstance" 1433
スクリプトは昇格して実行される必要があります。管理者として実行。
クレジットこのブログ投稿それは私を正しい方向に導いてくれました。