How do I automate maintenance in my IIS web app with Powershell?

Hi there,

I just wondering how to figure out with a solution that allows an application to signal to our load balancer that it is no longer part of a load balance pool...

I imagine to place a sort of empty "flag" file named "ExposeToLoadBalancer.flag" into the web application folder that will cause a particular URL within the application (that the loadbalancer monitors) to return a "200 OK" status code - and the load balancer will send  http requests to the application.

Instead, if the file does not exist, then the application will respond with "503 Under Maintenance", and this will cause the loadbalancer to no longer send new requests to the application.

This is the idea to automate the process.....but I'm not sure if it may be correct!

I'd like some help in order to create a script that can add the "ExposeToLoadBalancer.flag" to each application directory and one that will remove it.

Once a web application has been hidden from the loadbalancer, it can be stopped.
I assume to wait 30 seconds between "hiding" and stopping - just to allow any "in-flight" requests to complete.

Unluckly I'm not very confident with PS using IIS....could someone give me a hint?

Thanks in advance!
Alessandro ScafariaInfrastructure Premier Field AdministratorAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Dan McFaddenConnect With a Mentor Systems EngineerCommented:
Initial question:  What load balancer are you using?

I have done this in the following manner:

1. place a file (healthy.html) in the root of the website
2. configure LB to hit the file (
3. if a 200 is returned, site is live.  if anything other than a 200 is returned, drain connections to server and remove from group
4. wait until the LB GUI states that connections have gone to 0.  This is not always time dependent.

All I would do is to rename the file from "healthy.html" to "unhealthy.html"

The rename command is easy PS:

rename-item -path D:\websites\\healthy.html -newname unhealthy.html

Open in new window

- and back -

rename-item -path D:\websites\\unhealthy.html -newname healthy.html

Open in new window

As for automating it, I would need a little more info to answer that.
1. Do you have predefined maintenance windows?
2. Do you take servers out of service at predefined times?
3. Do you realty need to take the website offline (shut it down)?
4. Would this be for a code update maintenance or OS type work... patching?

Alessandro ScafariaInfrastructure Premier Field AdministratorAuthor Commented:
Hi Dan,

thank you so much for your reply!

I'm using ARR as load balancer for my Web Farm built with IIS 7.5.

I'll try to answer properly to your bottom questions in only one shot:

Unfortunately, releases of web applications are frequently. During release, I usually hide and stop the existing version of an application, then install the new version and then start and expose the new version. All the while maintaining the old version (in case I need to roll-back).

I try to achieve this through folder structures that have a version number in their path and through website names that also contain a version number.

In your opinion, how should I "force" to remember what are the actual active sites in the script? This is important when, for example, a release of a website that was not successful, and that the latest version of one of the applications is not actually the "live" version.

Usually my maintenance window requires also a reboot. Is it possible to automate it too?

Many thanks for your help!!
Dan McFaddenSystems EngineerCommented:
Here are a few articles on IIS and ARR:

Health Check:

List of links:

I understand your process, but I'm not sure I get the "how should I force" statement.  Let me restate your statement to see if I understand it:

1. script sets 1 or more ARR load balanced websites to "unhealthy"
2. script should know or have a list of active websites
3. script should reboot server

So if that is correct, you can have, in a text file, the paths to each website on the web server.  The script can loop thru this text file and do a predefined set of actions.

That much is straight forward, without knowing your structure and naming format, I can't say much more.

How does IIS get (re-)configured so that it knows what files to serve up upon request?

Alessandro ScafariaInfrastructure Premier Field AdministratorAuthor Commented:
Thank you Dan,

I appreciate! Here is an ideal example of my folder structure:





Open in new window

You made my point with your statements!

I'm not use how to help you when you're saying: "How does IIS get (re-)configured so that it knows what files to serve up upon request?"....

Thanks in advance!
Dan McFaddenSystems EngineerCommented:
My question was, when you deploy a new version structure, what happens to the older versions?

-Do they stay in the "getinto2..." directory?
- Which of the directories in your example is the web root for the website?
- When someone goes to one of your websites, how do they know or how does IIS know, what URL to return?
- Are you using URL rewriting in ARR?

Have you experimented with web Deploy from MS?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.