Monday, 18 March 2013

Make server unavailable gracefully using Powershell in ARR

Another tidbit for those of us using the ARR framework under IIS.

I've had cause over the last few weeks to manipulate the ARR properties using Powershell. Some of this is covered by the WebFarmSnapIn, but much of it still is not.

Today, I found the missing link for manipulating the entire ARR property schema, and invoking the methods container therein.

http://forums.iis.net/t/1156563.aspx

Below, is a copy and paste of that forum post in case it ever goes offline.

Preparation

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$mgr = new-object Microsoft.Web.Administration.ServerManager
$conf = $mgr.GetApplicationHostConfiguration()
$section = $conf.GetSection("webFarms")
$webFarms = $section.GetCollection()
$webFarm = $webFarms[0]$servers = $webFarm.GetCollection()
$server = $servers[0]$arr = $server.GetChildElement("applicationRequestRouting")

Simple queries

$counters = $arr.GetChildElement("counters")
We now have everything we need to have some real fun! To get the health status of the server we can do
$counters.GetAttributeValue("isHealthy")
$counters.GetAttributeValue("state")
All the counters can be listed with that
$counters.Attributes | Format-List

Changing state

That is all real cool but we also want to change the health status of a server
$method = $arr.Methods["SetHealthy"]
# $method = $arr.Methods["SetUnhealthy"]
Get the instance of the method
$methodInstance = $method.CreateInstance()
Execute the method
$methodInstance.Execute()
We can also change the state of the server
Get the method we want
$method = $arr.Methods["SetState"]
Get the instance of the method
$methodInstance = $method.CreateInstance()
Set the input value
$methodInstance.Input.Attributes[0].Value = 0
Execute the method
$methodInstance.Execute()
So far I've seen that these input values yields to :
0 -> Available
1 -> Drain
2 -> Unavailable
3 -> Unavailable
2 and 3 yields to Unavailable so I guess one is for "Make Server Unavailable Gracefully" and the other is for Make Server Unavailable Immediately"