Add / Remove Web Farm Server

on Friday, June 6, 2014

Here’s a couple of PowerShell functions to enable and disable a web farm server on IIS 7+. They work directly with the applicationHost.config file so they aren’t dependent on any particular version of IIS. This also means that they can be used to update remote IIS installations.

Sorry for the misleading title, but it’s better to enable/disable a web farm server rather than add/remove it. When you remove a server from a web farm any requests currently being processed by it will be lost. When disabling the server, the hanging requests will finish processing.

<#
.SYNOPSIS
Checks if an application server is listed in web farm. And, if it is, is it enabled.

Used to check if a server is 'enabled' on a web farm.
>
Function Test-WebFarmServerEnabled {
[CmdletBinding()]
Param(
[string]$ProxyServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[string]$WebFarmName,
[Parameter(Mandatory = $true)]
[string]$AppServerName
)
$configPath = Get-WebConfigPath $ProxyServerName
$appHost = Read-WebConfig -ConfigPath $configPath

$webFarm = $appHost.configuration.webFarms.webFarm |? {$_.name -eq $WebFarmName}
$webFarmServer = $webFarm.server |? {$_.address -eq $AppServerName}

$enabled = [System.Convert]::ToBoolean($webFarmServer.enabled);
return $enabled;
}

<#
.SYNOPSIS
Sets an application server in a web farm to either enabled or disabled. By setting the -Enabled
parameter the server will be enabled. If the parameter is missing the server will
be disabled.

Use when enabling or disabling servers in a web farm. This doesn't actually add or
remove a server to a web farm.

.EXAMPLE
To enable, set the -Enabled parameter to $true

Set-WebFarmServerEnabled -ProxyServerName "WebProxy1" -WebFarmName "WebFarm-Dev" `
-AppServerName "WebApp1" -Enabled $true

.EXAMPLE
To disable set the -Enabled parameter to $false

Set-WebFarmServerEnabled -ProxyServerName "WebProxy1" -WebFarmName "WebFarm-Dev" `
-AppServerName "WebApp1" -Enabled $false
>
Function Set-WebFarmServerEnabled {
[CmdletBinding()]
Param(
[string] $ProxyServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[string] $WebFarmName,
[Parameter(Mandatory = $true)]
[string] $AppServerName,
[Parameter(Mandatory = $true)]
[bool] $Enabled,
[bool] $SkipLoadBalancingDelay = $false
)
$configPath = Get-WebConfigPath $ProxyServerName
$appHost = Read-WebConfig -ConfigPath $configPath

$webFarm = $appHost.configuration.webFarms.webFarm |? {$_.name -eq $WebFarmName}
$webFarmServer = $webFarm.server |? {$_.address -eq $AppServerName}

if($Enabled) {
$value = "true";
} else {
$value = "false";
}
$webFarmServer.enabled = $value;

Write-Warning "Updating Proxy $ProxyServerName - Setting webfarm $WebFarmName's server $AppServerName to enabled='$value'"
Save-WebConfig -WebConfig $appHost -ConfigPath $configPath
Write-Host "Updated Proxy $ProxyServerName - Setting webfarm $WebFarmName's server $AppServerName to enabled='$value'"

if($SkipLoadBalancingDelay -eq $false) {
Write-Warning "Waiting 10 seconds to let the proxy server handle any hanging requests or start load balancing"
Start-Sleep 10;
}
}

Get-WebConfigPath, Read-WebConfig, and Save-WebConfig are described in this previous post.

Background

In IIS 7 there were Web Farm PowerShell Cmdlets. There was a separate package for them because in IIS 7 the Web Farm Framework (WFF) was a separate package.

In IIS 8, WFF became integrated into Application Request Routing, but the PowerShell Cmdlet’s didn’t get integrated into the WebAdministration module. Nor, was there a separate PowerShell module made for ARR.

It also wasn’t possible to use the IIS 7 PowerShell Cmdlets on IIS 8 because they called a particular set of dlls that were installed by IIS 7’s Web Farm Framework. Those dlls were integrated into other packages and aren’t available on IIS 8.

0 comments:

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.