サンプル:RemoveCacheHost スクリプト

ここでは、サンプルの Windows PowerShell スクリプト RemoveCacheHost.ps1 について説明します。このスクリプトは、キャッシュ ホストをキャッシュ クラスターから自動的に削除します。このスクリプトで使用されるコマンドについては、「インストールと構成の自動化」を参照してください。RemoveCacheHost サンプル スクリプトにより、以下の操作が実行されます。

  • 削除の準備としてキャッシュ ホストを停止します。

  • キャッシュ ホストをキャッシュ クラスターから登録解除します。

  • キャッシュ サービスの構成を削除します。

  • キャッシュ管理機能の構成を削除します。

  • AppFabric キャッシュ用に組み込まれた Windows ファイアウォール ポリシーを無効にします。

サンプル: RemoveCacheHost スクリプト

RemoveCacheHost スクリプトを使用するには、まず次の Windows PowerShell スクリプトの内容をテキスト ファイルにコピーし、RemoveCacheHost.ps1 という名前でファイルを保存します。次に、スクリプトの後の説明を確認して、スクリプトをカスタマイズして実行する方法を理解します。

param([switch]$RemoveCacheCluster, [string]$Pvd, [string]$ConnStr)

##########################
# Customizable Variables #
##########################
$provider = "System.Data.SqlClient"
#$provider = "XML"
$host_name = $env:COMPUTERNAME
$cache_port = 22233

# If System.Data.SqlClient:
$database_name = "CacheClusterConfigurationDB"
$database_server = "SQLServer1"
# If XML Provider:
$share_location = "\\Server1\CacheConfigShare"

##############
# Initialize #
##############

Import-Module DistributedCacheAdministration
Import-Module DistributedCacheConfiguration

$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
}

#####################
# Remove Cache Host #
#####################

Write-Host "`nUse-CacheCluster -Provider $provider -ConnectionString"`
   "`"$connection_string`"" -ForegroundColor Green
Use-CacheCluster -Provider $provider -ConnectionString $connection_string

#Make sure the cache host is stopped
$Get_CacheHost_Command = Get-CacheHost -HostName $host_name -CachePort $cache_port

if ($Get_CacheHost_Command.Status -eq "Up")
{
   Write-Host "`nStop-CacheHost -HostName $host_name -CachePort $cache_port" -ForegroundColor Green
   Stop-CacheHost -HostName $host_name -CachePort $cache_port
}

$Get_CacheHost_Command = Get-CacheHost -HostName $host_name -CachePort $cache_port

if ($Get_CacheHost_Command.Status -eq "Down")
{
   Write-Host "`nUnregister-CacheHost -Provider $provider -ConnectionString `"$connection_string`" " `
      "-HostName $host_name -RemoveServicePermissions" -ForegroundColor Green
   Unregister-CacheHost -Provider $provider -ConnectionString $connection_string `
      -HostName $host_name -RemoveServicePermissions
   
   Write-Host "`nRemove-CacheHost" -ForegroundColor Green
   Remove-CacheHost
   
   Write-Host "`nRemove-CacheAdmin" -ForegroundColor Green
   Remove-CacheAdmin
   
   ########################
   # Remove Cache Cluster #
   ########################
   
   # Look for a parameter that specifies this is a new cache cluster
   if ($RemoveCacheCluster)
   {
      Write-Host "`nRemove_CacheCluster -Provider $provider -ConnectionString "`
         "`"$connection_string`" -Force" -ForegroundColor Green
      Remove-CacheCluster -Provider $provider -ConnectionString $connection_string -Force
   }
   
   ##########################
   # Configure the Firewall #
   ##########################
   Write-Host "`nConfigure the firewall..." -ForegroundColor Green
   netsh advfirewall firewall set rule `
      group="Windows Server AppFabric: AppFabric Caching Service" new enable=No | Out-Null
   
   # Uncomment the following lines only if other services do not need Remote Service Management.
   #netsh advfirewall firewall set rule `
   #   name=\"Remote Service Management (RPC)\" profile=domain new enable=No | Out-Null
   #netsh advfirewall firewall set rule `
   #   name=\"Remote Service Management (RPC-EPMAP)\" profile=domain new enable=No | Out-Null
   #netsh advfirewall firewall set rule `
   #   name=\"Remote Service Management (NP-In)\" profile=domain new enable=No | Out-Null 
}
else
{
   Write-Host "`nUnable to stop the host $host_name (Port:$cache_port)`n`n" -ForegroundColor Red
}

RemoveCacheHost スクリプトのカスタマイズ

このスクリプトを使用できるようにカスタマイズするには、2 つのオプションがあります。まず、スクリプトの "Customizable Variables" セクションにある変数の値は、変更できます。たとえば、XML プロバイダーを使用している場合は、$provider 変数を "XML" に設定し、$share_location 変数を構成ストアに使用するネットワーク共有に設定します。固有の設定に基づいてその他の変数をカスタマイズします。$database_name 変数と $database_server 変数は、System.Data.SqlClient プロバイダーを使用している場合のみ必須です。$share_location 変数は、XML プロバイダーを指定する場合のみ必須です。

もう 1 つのオプションは、スクリプトの Pvd パラメーターと ConnStr パラメーターを使用して、プロバイダーと接続文字列をコマンド ラインから手動で指定する方法です。これらのパラメーターにより、内部の変数設定が上書きされます。

RemoveCacheHost スクリプトの実行

次のコマンドは、キャッシュ クラスターからローカル サーバーを削除します。

RemoveCacheHost

次のコマンドは、キャッシュ クラスターからローカル サーバーを削除し、キャッシュ クラスターも削除します。キャッシュ クラスターから最後のキャッシュ ホストを削除する場合、この処理を実行できます。

RemoveCacheHost -RemoveCacheCluster

次の例は、プロバイダーと接続文字列をコマンド ラインから手動で指定する方法を示します。

RemoveCacheHost.ps1 -Pvd System.Data.SqlClient -ConnStr "DataSource=SQLServer1;Initial Catalog=CustomConfigDB;Integrated Security=True"

スクリプトでは指定したキャッシュ ホストを停止できない場合があることに注意してください。その場合は、スクリプトでキャッシュ ホストを削除する前に、Stop-CacheCluster コマンドでキャッシュ クラスターを停止する必要があります。

関連項目

概念

インストールと構成の自動化

  2012-03-05