Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make configuring an IIS Server Farm easier
function Add-ServerFarm {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string[]]$Servers,
[string]$HostName,
[string]$RootPath,
[string]$HealthCheckPath='healthcheck',
[string]$HealthCheckUrl,
[switch]$IncludeNonSSL=$true,
[switch]$IncludeSSL=$true,
[string]$HealthCheckStatus='200',
[string]$HealthCheckInterval='00:00:10',
[string]$HealthCheckTimeout='00:00:02',
[string]$PSPath = 'MACHINE/WEBROOT/APPHOST'
)
if (!$HealthCheckUrl) {
if ($IncludeSSL) {
$HealthCheckUrl = "https://$HostName"
} else {
$HealthCheckUrl = "http://$HostName"
}
if ($RootPath) {
$HealthCheckUrl += "/$RootPath"
}
if ($HealthCheckPath) {
$HealthCheckUrl += "/$HealthCheckPath"
}
}
Add-WebConfigurationProperty -pspath $PSPath -filter "webFarms" -name "." -value @{name=$Name}
Set-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']/applicationRequestRouting/healthCheck" -name "url" -value $HealthCheckUrl
Set-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']/applicationRequestRouting/healthCheck" -name "interval" -value $HealthCheckInterval
Set-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']/applicationRequestRouting/healthCheck" -name "timeout" -value $HealthCheckTimeout
Set-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']/applicationRequestRouting/healthCheck" -name "statusCodeMatch" -value $HealthCheckStatus
Set-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']/applicationRequestRouting/loadBalancing" -name "algorithm" -value "WeightedRoundRobin"
foreach($server in $Servers)
{
Add-WebConfigurationProperty -pspath $PSPath -filter "webFarms/webFarm[@name='$Name']" -name "." -value @{address=$server}
}
$arrRewriteRuleName = "ARR_{0}_loadBalance" -f $Name
if ($RootPath) {
$ruleMatch = "{0}/*" -f $RootPath
} else {
$ruleMatch = '*'
}
if ($IncludeNonSSL) {
# Add the Non SSL Rewrite Rule
Add-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules" -name "." -value @{name=$arrRewriteRuleName;patternSyntax='Wildcard';stopProcessing='True'}
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/match" -name "url" -value $ruleMatch
Add-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/conditions" -name "." -value @{input='{HTTP_HOST}';matchType='Pattern';pattern=$HostName}
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/action" -name "type" -value "Rewrite"
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/action" -name "url" -value "http://$Name/{R:0}"
}
# Add the SSL Rewrite rule
$arrRewriteRuleName += "_SSL"
Add-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules" -name "." -value @{name=$arrRewriteRuleName;stopProcessing='True'}
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/match" -name "url" -value $ruleMatch
Add-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/conditions" -name "." -value @{input='{HTTPS}';pattern='on'}
Add-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/conditions" -name "." -value @{input='{HTTP_HOST}';matchType='Pattern';pattern=$HostName}
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/action" -name "type" -value "Rewrite"
Set-WebConfigurationProperty -pspath $PSPath -filter "system.webServer/rewrite/globalRules/rule[@name='$arrRewriteRuleName']/action" -name "url" -value "https://$Name/{R:0}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment