Поділитися через


Sample: RemoveCacheHost Script

This section provides a sample Windows PowerShell script, RemoveCacheHost.ps1. This script automatically removes a cache host from a cache cluster. For an explanation of the commands that are used in this script, see Automated Installation and Configuration. The RemoveCacheHost sample script performs the following actions:

  • Attempts to stop the cache host to prepare to remove it.

  • Unregisters the cache host from the cache cluster.

  • Removes the configuration of the Caching Service.

  • Removes the configuration of the Cache Administration feature.

  • Disables the built-in Windows firewall policy for AppFabric Caching.

Sample: RemoveCacheHost Script

To use the RemoveCacheHost script, first copy the contents of the following Windows PowerShell script to a text file, and save the file as RemoveCacheHost.ps1. Then review the information following the script to understand how to customize and run the script.

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
}

Customizing the RemoveCacheHost Script

To customize this script for your use, you have two options. First, you can change the values for the variables in the "Customizable Variables" section of the script. For example, if you are using the XML provider, set the $provider variable to "XML" and set the $share_location to the network share that should be used for the configuration store. Customize the other variables based on your specific settings. Note that the $database_name and $database_server variables are required only when you are using the System.Data.SqlClient provider. The $share_location variable is required only when you specify the XML provider.

Another option is to use the script parameters Pvd and ConnStr to manually specify the provider and connection string from the command-line. These parameters override the internal variable settings.

Running the RemoveCacheHost Script

The following command removes the local server from the cache cluster.

RemoveCacheHost

The following command removes the local server from the cache cluster and also removes the cache cluster. This action can be taken when the last cache host is removed from a cache cluster.

RemoveCacheHost -RemoveCacheCluster

The following example shows how to manually specify the provider and connection string from the command-line.

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

Note that sometimes the script will be unable to stop the specified cache host. In those instances, you will have to stop the cache cluster with the Stop-CacheCluster command before removing the cache host with the script.

See Also

Concepts

Automated Installation and Configuration

  2012-10-26