Programming Blog

Jeremy Morgan

Mostly Coherent Ramblings of a Silicon Forest Software Developer

Using DISM to Create a Repeatable IIS Installation

Repeatable installs are all the rage in Devops these days. As developers we have this “automate everything” mentality, and for good reason. In this article I’ll show you how you can do that with IIS installation as well. There’s no reason to go hunting and pecking around the GUI every time you need to do this. This is the just one of many ways to automate IIS installs, which I’ll be covering in the next few weeks.

I also cover this in depth in my latest Pluralsight course on Installing IIS.

Why would I ever need to do this?

"How To Create Repeatable Installs in IIS"

Most of the time administrators probably have a task like “throw IIS on the server and make sure the application works” and they do just that. That’s how I used to do it too. I was working on a project many years ago that required us to spin up VM servers on a virtual network as a “stack”.

This stack was a small snapshot of our production network, so to make sure our software worked with everyone’s updates, we had to build a new stack a few times a week.

A few times a week, we’d have to click through the GUI install and remember each box to check. Yeah, you can get good at it but it’s a waste of time.

Naturally I had to find a way to automate it, and break out some PowerShell. You do this, because you save time that can be spent doing fun stuff. So let’s learn how to create automated, repeatable IIS installations.

Getting Started

So you’re an administrator for ACME Widgets and you want to create a custom install of IIS and make it automated. You create a new server and are ready to go.

To find out if IIS is installed, we’ll run a command called DISM. DISM is a tool to build and service Windows images, it’s mostly used to create and maintain images for Virtual Machines, it’s extremely powerful, but we’ll cover that in a future article. For now we’ll use it to check features on the machine.

Open up a command prompt, and run the following command:

1
dism /online /get-featureinfo /featurename:IIS-WebServer 

As I run it on my virtual machine, I can see that IIS is not yet installed.

Repeatable Installs in IIS

You can also find a list of everything that’s enabled with this command:

1
DISM /online /get-features /format:table | find "Enabled"

And you’ll see something like this:

Repeatable Installs in IIS

by piping the output into find and searching for enabled we can get a full list of features that are already enabled on the server.

We can also quickly see if a single feature is enabled like we did before, but piping the results into find and checking the state:

1
dism /online /get-featureinfo /featurename:IIS-WebServer | find "State"

Repeatable Installs in IIS

Here we can quickly and clearly see IIS is not installed.

Creating a Batch File

So now that we know we can get this type of output, we’re going to build a little batch file to do it, and we’ll run the command a little different this time. This is pretty light programming, but it will be a set of tests to see if particular items are installed.

Type in notepad webinstall.bat and let’s create a batch file. Add the following:

1
2
@ECHO OFF
dism /online /get-featureinfo /featurename:IIS-WebServer | find "State : Disabled" > nul

We’re typing in dism, online, get-featureinfo, featurename, then piping that to find. We’ll look for state saying disabled, and if that phrase is found, that means IIS is disabled, or not installed. That will change our error level, which we’ll add next.

1
2
3
if %ERRORLEVEL% == 0 (
echo "IIS Web Server is not installed"
)

If our error level is set to zero, that means the phrase state enabled was found in our output. Let’s test it out.

Repeatable Installs in IIS

Now in our batch file we can see that IIS is not installed, and we have a block inside the statement to install IIS.

We could do that with a block of code like this:

1
2
3
4
if %ERRORLEVEL% == 0 (
echo "IIS Web Server is not installed"
dism /online /enable-feature /featurename:IIS-WebServer /all
)

This is the first basic IIS install script. At the end we can put a check to see if it’s installed:

1
dism /online /get-featureinfo /featurename:IIS-WebServer | find "State"

The finished batch file is available here.

Running the Script

Now after we run the file, it should do the following set of steps:

  • Check to see if IIS is installed
  • If it’s not installed, install it
  • Check again to see if it’s installed.

So the end result is, if IIS is installed it does nothing, if it’s not installed then adds it, and looks like this:

Repeatable Installs in IIS

This small, simple script can make sure IIS is installed on the machine. And it’s repeatable.

Customizing the Install

Of course as you’re building your script, you may need to install other IIS modules, chances are it won’t be just a plain IIS install.

If you run the following command, you can see everything related to IIS that can be installed here:

1
dism /online /get-features | find "IIS"

This is a good way to get a list of IIS features available:

Repeatable Installs in IIS

So, if you’d like to add more IIS features, it’s as easy as making more loops to check for them:

ASP.NET:

1
2
3
4
5
6
dism /online /get-featureinfo /featurename:IIS-ASPNET45 | find "State : Disabled" > nul

if %ERRORLEVEL% == 0 (
echo "ASPNET is not installed. We should install it."
dism /online /enable-feature /featurename:IIS-ASPNET45 /all
)

HTTP Redirect:

1
2
3
4
5
6
dism /online /get-featureinfo /featurename:IIS-HttpRedirect | find "State : Disabled" > nul

if %ERRORLEVEL% == 0 (
echo "HTTP Redirect is not installed . We should install it."
dism /online /enable-feature /featurename:IIS-HttpRedirect /all
)

HTTP Logging:

1
2
3
4
5
6
dism /online /get-featureinfo /featurename:IIS-HttpLogging | find "State : Disabled" > nul

if %ERRORLEVEL% == 0 (
echo "HTTP Logging is not installed . We should install it."
dism /online /enable-feature /featurename:IIS-HttpLogging /all
)

You get the idea. You can build a custom installation that can be run from a batch file, quickly and easily.

Conclusion

So now you’re set to create a neat little batch file you can run on any machine to install IIS to 100 different VMs if you want to. You can put this on an Azure stack, or run it on a development machine. As I stated I cover this and many other repeatable installations in my latest course on Pluralsight. It covers ways to effectively install IIS at a large scale.

You can even run DISM remotely or on VM Images that aren’t even booted. It’s a great tool to get to know.


Questions or Comments about this article? I’d love to hear it. Leave them here!

Unit Testing With Dotnet Core

So you’ve just started building .Net Core applications and really starting to gain some traction. You quickly learn how mature and thorough the .Net Core framework is becoming and think “I need to start writing some unit tests for this!”. As it turns out, it’s super easy and very intuitive, especially for C# developers.

Set Up Easy File Sharing With Samba

In this tutorial, I’ll show you how easy it is to setup file sharing on your network using SAMBA. You can easily share files between Linux and Windows machines with a pretty minimal amount of setup.

Windows to Go With the Spyrus USB Drive

What we need is a good bootable live USB stick for Windows. I’ve said this many times over the years, and hacked together things to make that very thing happen, but nothing that worked really well. So when the folks at Spyrus sent me a Windows to Go USB to check out, I was pretty excited.

Which Distribution of Linux Should I Use?

I’m often asked this question: “hey, you’re a Linux guy right? What Linux should I use? I have this friend who recommends _____ and I want to know what you think?” I usually reply with the same question: what do you want to do? So I decided to make a blog post about it that I can send people instead.

My Latest Virtualization Setup

Like many geeks of the time I spent the 90s and 2000s with at least 2 or 3 old computers in a closet, connected by a switch running various operating systems with various services running on them. Giant, loud, clunky machines whirring away.

Load Testing Your IIS Web Server

All the theory, calculations, and estimations in the world aren’t going to tell you how your website will truly perform under a load. If you’re deploying a new server, or doing any kind of performance enhancements you don’t want to test your results in production. It’s always a good idea to see how your system behaves before your visitors do. To do that, you can use a load testing tool, and here are a few I use quite frequently.

Update: I’ve featured these tools is my latest IIS course on Pluralsight, IIS Administration in Depth, check it out!

How to Install Microsoft SQL Server on Ubuntu Linux in 5 Minutes

I must admit I was surprised when I learned that Microsoft SQL Server would be availble in Linux. They’ve been pushing the open source initiative hard, but I didn’t expect something this big. Oh yeah, Visual Studio is now available for Mac as well. I just saw a pig flying by.

While MS-SQL is not open source they have made it available to run on open source platforms such as Linux and OSX, which I can imagine took a ton of work. So I decided to take advantage of this new option and try it out. It works great! It took 5 minutes to install. Here’s how you can do it too. Note that you will need a server with 3.5 gigs of RAM for this.

Intro to Application Pools in IIS

Application Pools in IIS can be somewhat of a mystery. Since they’re created for you automatically many times Administrators don’t give them a second thought. But understanding application pools is crucial to having a good running IIS server.

I cover this and other IIS Administration topics in my IIS Administration Fundamentals course at Pluralsight. Check it out!