Steve Fenton

Change availability of web farm servers with PowerShell

I’m running an IIS site with ARR as a load balancer. It’s pretty common to want to take a server out of balance or to put it back in using a script. The below script takes the first server out of balance by setting enabled = $false. The other servers are left in. You could make this more complex by making the script conditional rather than hard-coding the values.

$serverFarm = 'Sample'

Set-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$serverFarm']" `
    -Value @(
        @{ address = '192.168.236.121'; enabled = $false },
        @{ address = '192.168.236.122'; enabled = $true },
        @{ address = '192.168.236.123'; enabled = $true },
        @{ address = '192.168.236.124'; enabled = $true },
        @{ address = '192.168.236.125'; enabled = $true }
    )

You need to provide the complete list of servers each time, with just the enabled flag updated. So, to put the first server back into balance and remove the second one, it just needs to be changed to:

$serverFarm = 'Sample'

Set-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$serverFarm']" `
    -Value @(
        @{ address = '192.168.236.121'; enabled = $true },
        @{ address = '192.168.236.122'; enabled = $false },
        @{ address = '192.168.236.123'; enabled = $true },
        @{ address = '192.168.236.124'; enabled = $true },
        @{ address = '192.168.236.125'; enabled = $true }
    )

And we can put everything back in balance with:

$serverFarm = 'Sample'

Set-WebConfiguration -PSPath 'MACHINE/WEBROOT/APPHOST' `
    -Filter "webFarms/webFarm[@name='$serverFarm']" `
    -Value @(
        @{ address = '192.168.236.121'; enabled = $true },
        @{ address = '192.168.236.122'; enabled = $true },
        @{ address = '192.168.236.123'; enabled = $true },
        @{ address = '192.168.236.124'; enabled = $true },
        @{ address = '192.168.236.125'; enabled = $true }
    )

Written by Steve Fenton on