サンプル:AddCacheHost スクリプト
ここでは、サンプルの Windows PowerShell スクリプト AddCacheHost.ps1 について説明します。このスクリプトを使用すると、キャッシュ ホストをキャッシュ クラスターに追加するために必要な手順が自動化されます。このスクリプトで使用されるコマンドについては、「インストールと構成の自動化」を参照してください。AddCacheHost サンプル スクリプトにより、以下の操作が実行されます。
NewCacheCluster
パラメーターが指定されると新しいキャッシュ クラスターを作成します。指定されたキャッシュ クラスターにローカル サーバーを登録します。
ローカル サーバー上でキャッシュ サービスを構成します。
ローカル サーバー上でキャッシュ管理機能を構成します。
AppFabric キャッシュ用に組み込まれた Windows ファイアウォール ポリシーを有効にします。
クラスターが稼働している場合はキャッシュ ホストを起動します。
ヒント
Windows PowerShell のスクリプトとその実行方法の詳細については、「Windows PowerShell スクリプト」を参照してください。
AddCacheHost スクリプトの前提条件
このスクリプトを実行するには、Microsoft AppFabric 1.1 for Windows Server とキャッシュ サービス機能、およびキャッシュ管理機能をインストールする必要があります。キャッシュ構成ストアの場所も設定する必要があります。組み込みプロバイダーには、XML および System.Data.SqlClient という 2 つがあります。ネットワーク ファイル共有の設定方法の詳細については、「共有フォルダー ベースのクラスター構成」を参照してください。SQL Server の要件の詳細については、「SQL Server ベースのクラスター構成」を参照してください。
構成ストアに SQL Server を使用している場合は、Windows PowerShell を使用して構成データベースを作成することもできます。これを行うには、SQL Server Windows PowerShell プロバイダーがインストールされている必要があります。また、SQL Server に接続する権限と新しいデータベースを作成する権限を持っている必要があります。次のスクリプトによって、指定された SQL Server でデータベースが作成されます。
param([string]$server="NOTSPECIFIED", [string]$database_name = "CacheClusterConfigurationDB", `
[string]$database_path="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\")
#################
# Set Variables #
#################
# Parameters specifying configuration database details
if ($server -eq "NOTSPECIFIED")
{
# Use local server if none is specified
$server = $env:COMPUTERNAME
}
$connection_string = "Data Source=" + $server + `
";Initial Catalog=" + $database_name + `
";Integrated Security=True"
Write-Host "`nConnection String:`n$connection_string`n" -ForegroundColor Green
###################################################
# Create the Cache Cluster Configuration Database #
###################################################
$database_file_path = "N'" + $database_path + $database_name + ".mdf'"
$database_log_path = "N'" + $database_path + $database_name + "_log.ldf'"
$Database_Exists_Command = "select count(*) from sys.databases where name = '" + $database_name + "'"
$Database_Exists_Result = Invoke-Sqlcmd $Database_Exists_Command -ServerInstance $server
Write-Host "`nCreating Configuration Database:" -ForegroundColor Green
if ($Database_Exists_Result.Item(0) -eq 0)
{
$Create_DB_Command = "CREATE DATABASE [" + $database_name + "] ON PRIMARY " + `
"( NAME = N'" + $database_name + "', " + `
"FILENAME = " + $database_file_path + ", SIZE = 2048KB , FILEGROWTH = 1024KB ) " + `
"LOG ON ( NAME = N'" + $database_name + "_log'," + `
"FILENAME = " + $database_log_path + ", SIZE = 1024KB , FILEGROWTH = 10%)"
Write-Host "$Create_DB_Command`n" -ForegroundColor Green
Invoke-Sqlcmd $Create_DB_Command -ServerInstance $server
}
else
{
Write-Host "Database $database_name already exists on $server.`n" `
-ForegroundColor Green
}
このスクリプトを使用するには、SQL Server Windows PowerShell スナップインが読み込まれている必要があります。これを行うには、次のコマンドを使用します。
Add-PSSnapin SqlServerCmdletSnapin100
このスクリプトが CreateCacheConfigDB.ps1 という名前で保存された場合、次の呼び出しによって SQL Server SQLServer1
上に CacheClusterConfigurationDB
という名前の新しいデータベースが作成されます。database_name パラメーターと database_path パラメーターの既定値を使用して、サーバー名のみを指定することもできます。
CreateCacheConfigDB.ps1 -server SQLServer1 -database_name "CacheClusterConfigurationDB" -database_path "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\"
サンプル: AddCacheHost スクリプト
AddCacheHost スクリプトを使用するには、まず次の Windows PowerShell スクリプトの内容をテキスト ファイルにコピーし、AddCacheHost.ps1 という名前でファイルを保存します。次に、スクリプトの後の説明を確認して、スクリプトをカスタマイズして実行する方法を理解します。
param([switch]$NewCacheCluster, [string]$Pvd, [string]$ConnStr)
##########################
# Customizable Variables #
##########################
$provider = "System.Data.SqlClient"
#$provider = "XML"
$host_name = $env:COMPUTERNAME
$service_account = "NT Authority\Network Service"
$starting_port = 22233
$cluster_size = "Small"
# If System.Data.SqlClient:
$database_name = "CacheClusterConfigurationDB"
$database_server = "SQLServer1"
# If XML Provider:
$share_location = "\\Server1\CacheConfigShare"
##############
# Initialize #
##############
Import-Module DistributedCacheAdministration
Import-Module DistributedCacheConfiguration
$cache_port = $starting_port + 0
$cluster_port = $starting_port + 1
$arbitration_port = $starting_port + 2
$replication_port = $starting_port + 3
$connection_string = ""
if ($provider -eq "System.Data.SqlClient")
{
$connection_string = "Data Source=" + $database_server + `
";Initial Catalog=" + $database_name + `
";Integrated Security=True"
}
if ($provider -eq "XML")
{
$connection_string = $share_location
}
# If provided, command-line parameters override
# internal script variables:
if ($Pvd)
{
$provider = $Pvd
}
if ($ConnStr)
{
$connection_string = $ConnStr
}
##############################
# Create a New Cache Cluster #
##############################
$Get_CacheClusterInfo_Command = Get-CacheClusterInfo -Provider $provider -ConnectionString $connection_string
# Look for a PowerShell script parameter that specifies this is a new cache cluster
if ($NewCacheCluster -and !$Get_CacheClusterInfo_Command.IsInitialized)
{
Write-Host "`nNew-CacheCluster -Provider $provider -ConnectionString "`
"`"$connection_string`" -Size $cluster_size" -ForegroundColor Green
New-CacheCluster -Provider $provider -ConnectionString $connection_string -Size $cluster_size
}
######################
# Add the Cache Host #
######################
Write-Host "`nRegister-CacheHost -Provider $provider -ConnectionString `"$connection_string`" "`
"-Account `"$service_account`" -CachePort $cache_port -ClusterPort $cluster_port "`
"-ArbitrationPort $arbitration_port -ReplicationPort $replication_port -HostName "`
"$host_name" -ForegroundColor Green
Register-CacheHost -Provider $provider -ConnectionString $connection_string -Account `
$service_account -CachePort $cache_port -ClusterPort $cluster_port -ArbitrationPort `
$arbitration_port -ReplicationPort $replication_port `
-HostName $host_name
Write-Host "`nAdd-CacheHost -Provider $provider -ConnectionString `"$connection_string`" "`
"-Account `"$service_account`"" -ForegroundColor Green
Add-CacheHost -Provider $provider -ConnectionString $connection_string -Account $service_account
Write-Host "`nAdd-CacheAdmin -Provider $provider -ConnectionString "`
"`"$connection_string`"" -ForegroundColor Green
Add-CacheAdmin -Provider $provider -ConnectionString $connection_string
Use-CacheCluster
##########################
# Configure the Firewall #
##########################
Write-Host "`nConfigure the firewall..." -ForegroundColor Green
netsh advfirewall firewall set rule `
group="Windows Server AppFabric: AppFabric Caching Service" new enable=Yes | Out-Null
netsh advfirewall firewall set rule `
name="Remote Service Management (RPC)" profile=domain new enable=Yes | Out-Null
netsh advfirewall firewall set rule `
name="Remote Service Management (RPC-EPMAP)" profile=domain new enable=Yes | Out-Null
netsh advfirewall firewall set rule `
name="Remote Service Management (NP-In)" profile=domain new enable=Yes | Out-Null
########################
# Start the Cache Host #
########################
# If the cluster is not running, don't start the cache host.
$running = 0
$Get_CacheHost_Command = Get-CacheHost
foreach ($cache_host in $Get_CacheHost_Command)
{
if ($cache_host.Status -eq "Up")
{
$running = 1
}
}
if ($running)
{
Write-Host "`nStart-CacheHost -HostName $host_name -CachePort $cache_port" -ForegroundColor Green
Start-CacheHost -HostName $host_name -CachePort $cache_port
}
else
{
Write-Host "`nNot starting new cache host; Cache Cluster is not running..." -ForegroundColor Green
}
Write-Host "`nGet-CacheHost`n" -ForegroundColor Green
Get-CacheHost
AddCacheHost スクリプトのカスタマイズ
このスクリプトを使用できるようにカスタマイズするには、2 つのオプションがあります。まず、スクリプトの "Customizable Variables" セクションにある変数の値は、変更できます。たとえば、XML プロバイダーを使用している場合は、$provider
変数を "XML"
に設定し、$share_location
変数を構成ストアに使用するネットワーク共有に設定します。固有の設定に基づいてその他の変数をカスタマイズします。$database_name
変数と $database_server
変数は、System.Data.SqlClient プロバイダーを使用している場合のみ必須です。$share_location
変数は、XML プロバイダーを指定する場合のみ必須です。
もう 1 つのオプションは、スクリプトの Pvd
パラメーターと ConnStr
パラメーターを使用して、プロバイダーと接続文字列をコマンド ラインから手動で指定する方法です。これらのパラメーターにより、内部の変数設定が上書きされます。
AddCacheHost スクリプトの実行
次のコマンドにより、新しいキャッシュ クラスターが作成され、ローカル サーバーがそのクラスターの新しいキャッシュ ホストとして構成されます。
AddCacheHost -NewCacheCluster
次のコマンドにより、ローカル サーバーが既存のキャッシュ クラスターのキャッシュ ホストとして構成されます。
AddCacheHost
次の例は、プロバイダーと接続文字列をコマンド ラインから手動で指定する方法を示します。
AddCacheHost.ps1 -NewCacheCluster -Pvd System.Data.SqlClient -ConnStr "Data Source=SQLServer1;Initial Catalog=CustomConfigDB;Integrated Security=True"
関連項目
概念
2012-03-05