Esempio: Script AddCacheHost
In questa sezione viene fornito uno script di Windows PowerShell di esempio, AddCacheHost.ps1. Questo script automatizza i passaggi richiesti per aggiungere un host di cache a un cluster di cache. Per una descrizione dei comandi utilizzati nello script, vedere Installazione e configurazione automatizzate. Lo script AddCacheHost di esempio esegue le seguenti azioni:
Crea un nuovo cluster di cache quando è specificato il parametro
NewCacheCluster
.Registra il server locale sul cluster di cache specificato.
Configura il Servizio di memorizzazione nella cache sul server locale.
Configura la funzione Amministrazione cache sul server locale.
Abilita i criteri di Windows Firewall predefiniti per memorizzazione nella cache di AppFabric.
Avvia l'host della cache se il cluster è attivo.
Suggerimento
Per ulteriori informazioni sugli script di Windows PowerShell e su come eseguirli, vedere Running Windows PowerShell Scripts.
Prerequisiti dello script AddCacheHost
Per eseguire questo script, è necessario installare Microsoft AppFabric 1.1 per Windows Server con le funzionalità Servizio di memorizzazione nella cache e Amministrazione cache. È inoltre necessario impostare il percorso dell'archivio di configurazione della cache. Sono disponibili due provider predefiniti: XML e System.Data.SqlClient. Per ulteriori informazioni sulla configurazione di una condivisione file di rete, vedere Configurazione dei cluster basati su cartelle condivise. Per ulteriori informazioni sui requisiti di SQL Server, vedere Configurazione di cluster basati su SQL Server.
Se si utilizza SQL Server per l'archivio di configurazione, è inoltre possibile utilizzare Windows PowerShell per creare il database di configurazione. A tale scopo è necessario che il provider di Windows PowerShell di SQL Server sia installato. È inoltre necessario disporre delle autorizzazioni appropriate per connettersi al server SQL e creare nuovi database. Lo script seguente crea un database nel server SQL specificato.
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
}
Per poter utilizzare questo script, è necessario che sia caricato lo snap-in Windows PowerShell di SQL Server. Per farlo, utilizzare il comando seguente:
Add-PSSnapin SqlServerCmdletSnapin100
Se lo script era stato salvato come CreateCacheConfigDB.ps1, la seguente chiamata crea un nuovo database denominato CacheClusterConfigurationDB
nel server SQL SQLServer1
. È anche possibile utilizzare i valori predefiniti dei parametri relativi a nome_database e percorso_database e specificare soltanto il nome del server.
CreateCacheConfigDB.ps1 -server SQLServer1 -database_name "CacheClusterConfigurationDB" -database_path "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\"
Esempio: Script AddCacheHost
Per utilizzare lo script AddCacheHost, copiare prima i contenuti del seguente script di Windows PowerShell in un file di testo e salvare il file con il nome AddCacheHost.ps1. Riesaminare quindi le informazioni che seguono lo script per comprendere come personalizzare ed eseguire lo 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
Personalizzazione dello script AddCacheHost
Per personalizzare lo script sono disponibili due opzioni. La prima opzione consiste nel modificare i valori delle variabili nella sezione "Customizable Variables" dello script. Ad esempio, se si utilizza il provider XML, impostare la variabile $provider
su "XML"
, quindi impostare $share_location
sulla condivisione di rete da utilizzare per l'archivio di configurazione. Personalizzare le altre variabili in base alle impostazioni specifiche. Si noti che le variabili $database_name
e $database_server
sono necessarie soltanto se si utilizza il provider System.Data.SqlClient. La variabile $share_location
è richiesta solo quando si specifica il provider XML.
L'opzione alternativa consiste nell'utilizzare i parametri Pvd
e ConnStr
dello script per specificare manualmente il provider e la stringa di connessione dalla riga di comando. Questi parametri hanno priorità sulle impostazioni interne delle variabili.
Esecuzione dello script AddCacheHost
Il seguente comando crea un nuovo cluster di cache e configura il server locale come nuovo host della cache su tale cluster.
AddCacheHost -NewCacheCluster
Il seguente comando configura il server locale come host di cache su un cluster di cache esistente.
AddCacheHost
Nell'esempio seguente viene descritto come specificare manualmente il provider e la stringa di connessione dalla riga di comando.
AddCacheHost.ps1 -NewCacheCluster -Pvd System.Data.SqlClient -ConnStr "Data Source=SQLServer1;Initial Catalog=CustomConfigDB;Integrated Security=True"
Vedere anche
Concetti
Installazione e configurazione automatizzate
2012-03-05