Windows PowerShell cmdlets are an administration-focused scripting language that helps you achieve control and productivity when managing your Windows Server AppFabric installation especially in the silent install/configuration scenarios wherein suppressing the Config dialog/screens is an important user experience.   The output results of one cmdlet can be piped to another cmdlet by combining the two statements with a single command and the “|” operator. You can invoke Windows PowerShell cmdlets either interactively using the Windows PowerShell console or through scripts. This brings the power of the .NET Framework to command-line users as well as script writers.  Just like the IIS interface, the AppFabric cmdlets can be used to manage remote services and applications.

As an administrator you may want to create scripts for specific core tasks. This saves time with duties that are done on a repetitive basis. It also makes the process less error prone since each time you will be running the same script file time through that task.

A commonly scripted task that you may want to manage through cmdlets is the AppFabric configuration process. We will now examine some core configuration operations that you can script for AppFabric to function correctly after the installation.   Before running these cmdlets, the following steps in the installation process must complete in order to properly configure AppFabric using scripting.

  • Databases have been prepared using the Initialize-ASMonitoringDatabase and Initialize-ASPersistenceSqlDatabase cmdlets. There may be permissions issues that need to be addressed before this will work that are particular to the security requirements at your installation.
  • The users of the Administrator role must have logon as a service privileges because they will be used to run the Event Collection and Workflow Management services.
  • The identity of the application pool used to run AppFabric applications must be a member of the logical AppFabric Users role.
  • The application pool must be a .NET Framework 4 application pool.

Note that in this article we will not discuss installing, configuring, and securing AppFabric on a domain or across a Web farm.

CMDLET OPERATION

ACTION

NET LOCALGROUP

Add the AS_Administrator user to the local Administrators group.

Get-WmiObject

Set the Event Collection Service and Workflow Management Service identities.

AppCmd

Add connection strings to the connection strings section in root web.config.

AppCmd

Set the default AppPool identity.

Initialize-ASPersistenceSqlDatabase

 

Creates and initializes a SQL Server persistence database on the specified database server if the specified database does not already exist on the server. The initialization process creates persistence-related artifacts (tables, views, stored procedures, and so on). If the database already exists on the server, the cmdlet initializes the database with the persistence artifacts. The cmdlet also adds any specified Windows groups to corresponding SQL Server roles.

Initialize-ASMonitoringSqlDatabase

Creates and initializes a SQL Server monitoring database on the specified database server if the specified database does not already exist on the server. The initialization process creates monitoring-related artifacts (tables, views, stored procedures, and so on). If the database already exists on the server, the cmdlet initializes the database with the monitoring artifacts. The cmdlet also adds any specified Windows groups to corresponding SQL Server roles.

Set-ASAppMonitoring

Enables monitoring and specifies the connection string that will be used to connect to the SQL Server monitoring database,

Set-ASAppSqlServicePersistence

Enables SQL Server persistence, specifies the connection string that will be used to connect to the SQL Server persistence database, and sets advanced persistence related parameters at the specified level.

 

These cmdlets can be run individually, or put inside a script file with a .ps1 extension to run as a Windows PowerShell Script. Here is an example of a script to assist in configuration of AppFabric.  The script has four functions and three sections.  Its main goals of this script are to get the user to enter a user name and password, change the logon user and password for a specified service, generate a SQL connection string to a specific database, and to add that connection string to the root Web.config file.

 ############################################################

# PowerShell script to configure

# Application Server Extensions for .NET 4

# Copyright (c) Microsoft Corporation.  All rights reserved.

############################################################

if ((Get-Command -Module ApplicationServer) -eq $null)

{

    Import-Module ApplicationServer # Application Server Extensions for .NET 4

}

 

############################################################

function GetUserCredential([string]$serviceName, [string]$serviceDescription, [string]$serviceUser)

{

    $credential = $host.ui.PromptForCredential($serviceName, $serviceDescription, $serviceUser, "")

    return $credential # PSCredential object

}

 

function SetServiceCredential([string]$serviceName, $credential)

{

    $domainAndUserName = $credential.GetNetworkCredential().Domain + "\" + $credential.GetNetworkCredential().UserName;

    $service = Get-WmiObject Win32_Service -filter "name='$serviceName'"

    $service.Change($null, $null, $null, $null, $null, $null, $domainAndUserName, $credential.GetNetworkCredential().Password) |out-null

    Restart-Service -name "$serviceName"

}

 

#### Gets a SQL connection string to the specified server and database.

function GetSqlConnectionString([string]$server, [string]$database)

{

    $builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder

    $builder.PSBase.DataSource = $server

    $builder.PSBase.InitialCatalog = $database

    $builder.PSBase.IntegratedSecurity = $true

    return [string] $builder.ToString()

}

 

#### Adds or updates the specified connection string setting in the specified .NET configuration file.

function UpdateConnectionString([string]$name, [string]$connectionString, [string]$providerName)

{ 

    & $appcmd set config /clr:4 /commit:WEBROOT /section:connectionStrings /+"[connectionString='$connectionString',name='$name',providerName='$providerName']" |out-null

}

 

##############################

### User defined variables ###

### List of variables used in the script. You can modify these variables to customize the script to the specifics of the environment.

##############################

 

$computer = "."

$appcmd = "$env:SystemRoot\system32\inetsrv\appcmd.exe"

 

### Event Collector Service (ECS) and Workflow Management Service (WMS) service names.

$ECS_ServiceName = "AppFabricEventCollectionService"

$WMS_ServiceName = "AppFabricWorkflowManagementService"

 

### Domain user, member of Application Server Administrators group.

### ECS and WMS will run under this identity.

$systemService_Domain ="corp"

$systemService_UserName = "dubAdmin"

 

### Monitoring database

$Monitoring_ConnectionStringName = "monitoringDB"

$Monitoring_ConnectionString = GetSqlConnectionString "AD-SQL" "monitoringDB"

 

$Monitoring_MonitoringLevel = "HealthMonitoring" # Used by the monitoring behavior.

 

### Persistence database

$Persistence_ConnectionStringName = "persistenceDB"

$Persistence_InstanceStoreName = "sqlPersistence"

$Persistence_ConnectionString = GetSqlConnectionString "AD-SQL" "persistenceDB"

 

###########################

### Collect credentials ###

### Calls the GetUserCredential function to obtain credentials from the user for use in the configuration of the system services (Workflow Management service and Event Collection service).

###########################

 

$systemService_Credentials = GetUserCredential "System Services" "Provide the credentials for the System Services user:" "$systemService_Domain\$systemService_UserName"

$systemService_Domain = $systemService_Credentials.GetNetworkCredential().Domain

$systemService_UserName = $systemService_Credentials.GetNetworkCredential().UserName

 

############################

### Update configuration ###

### Apply the configuration based on the variables and the collected information

############################

 

####Add the AppFabric Administrator user to the local Administrators group.

Write-Output "Adding the Administrator user to the local Administrators group..."

$oGroup = [ADSI]"WinNT://$computer/Administrators"

trap { continue } #'Administrator user already a member of the local Administrators group...';

& { $oGroup.Add("WinNT://$systemService_Domain/$systemService_UserName") }

 

####Set Event Collection service configuration.

Write-Output "Updating Event Collection service..."

SetServiceCredential $ECS_ServiceName $systemService_Credentials

 

####Set Workflow Management service configuration.

Write-Output "Updating Workflow Management service..."

SetServiceCredential $WMS_ServiceName $systemService_Credentials

 

####Add connection strings to the connection strings section in root web.config.Write-Output "Creating connection strings..."

UpdateConnectionString $Monitoring_ConnectionStringName $Monitoring_ConnectionString "System.Data.SqlClient" |out-null

UpdateConnectionString $Persistence_ConnectionStringName $Persistence_ConnectionString "System.Data.SqlClient" |out-null

 

####Create an Instance Store entry for the persistence connection string.

Write-Output "Creating Persistence Instance Store..."

Add-ASAppSqlInstanceStore -Name $Persistence_InstanceStoreName -ConnectionStringName $Persistence_ConnectionStringName -Root |out-null

 

####Set the persistence and monitoring behavior.

Write-Output "Creating default behaviors..."

Set-ASAppMonitoring -ConnectionStringName $Monitoring_ConnectionStringName -MonitoringLevel $Monitoring_MonitoringLevel -Root |out-null

Set-ASAppSqlServicePersistence -ConnectionStringName $Persistence_ConnectionStringName -Root -HostLockRenewalPeriod "00:00:20" -InstanceEncodingOption "GZip" -InstanceCompletionAction "DeleteNothing" -InstanceLockedExceptionAction "BasicRetry" |out-null