Sample: AddCacheHost Script (Windows Server Appfabric Caching)

This section provides a sample Windows PowerShell script, AddCacheHost.ps1. This script automates the steps that are required to add a cache host to a cache cluster. For an explanation of the commands that are used in this script, see Automated Installation and Configuration. The AddCacheHost sample script performs the following actions:

  • Creates a new cache cluster when the NewCacheCluster parameter is specified.

  • Registers the local server on the cache cluster specified.

  • Configures the Caching Service on the local server.

  • Configures the Cache Administration feature on the local server.

  • Enables the built-in Windows firewall policy for AppFabric caching.

  • Starts the cache host if the cluster is up.

Tip

To learn more about Windows PowerShell scripts and how to run them, see Running Windows PowerShell Scripts.

AddCacheHost Script Prerequisites

To run this script, you must install Windows Server AppFabric with the Caching Service and Cache Administration features. You also are required to set up the location for the cache configuration store. There are two built-in providers: XML and System.Data.SqlClient. For more information about how to set up a network file share, see Shared Folder-Based Cluster Configuration. For more information about the SQL Server requirements, see SQL Server-Based Cluster Configuration.

If you are using SQL Server for the configuration store, it is also possible to use Windows PowerShell to create the configuration database. This requires that you have the SQL Server Windows PowerShell provider installed. You must also have appropriate permissions for connecting to the SQL Server and creating new databases. The following script creates a database in the specified 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  
}

To use this script, you must have the SQL Server Windows PowerShell snapin loaded. This can be done with the following command.

Add-PSSnapin SqlServerCmdletSnapin100

If the script were saved as CreateCacheConfigDB.ps1, the following call creates a new database named CacheClusterConfigurationDB on the SQL Server SQLServer1. Note that you could also rely on the defaults for the database_name and database_path parameters and only specify the server name.

CreateCacheConfigDB.ps1 -server SQLServer1 -database_name "CacheClusterConfigurationDB" -database_path "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\"

Sample: AddCacheHost Script

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

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

Customizing the AddCacheHost 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 AddCacheHost Script

The following command creates a new cache cluster and configures the local server as a new cache host on that cluster.

AddCacheHost -NewCacheCluster

The following command configures the local server as a cache host on an existing cache cluster.

AddCacheHost

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

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

See Also

Concepts

Automated Installation and Configuration