Chapter 11. Web Server Administration
In this chapter, we will learn how to deal with Internet Information Services (IIS), the web server that ships with Windows servers. We will specifically look at the following topics:
- Installing IIS
- The WebAdministration module
- Starting, stopping, and restarting IIS
- Creating virtual directories and web applications
- Working with app pools
Installing IIS
Before we install IIS, we need to determine whether it is already installed. This is done differently in a client OS, such as Windows 8.1, rather than in a server OS such as Server 2012R2.
Detecting and installing IIS in Windows 8.1
In a client OS, the cmdlet to use is Get-WindowsOptionalFeature
, and the name of the feature to look for is IIS-WebServerRole
. The –Online
switch tells the cmdlet to examine the OS running on the computer rather than looking in a Windows image. In the following screenshot, you can see that IIS is not enabled on my Windows 8.1 computer:
To install IIS, you can use the Enable-WindowsOptionalFeature
cmdlet using the IIS-WebServerRole
feature name again and the –Online
switch. I also added the –All
switch to tell Enable-WindowsOptionalFeature
to enable any required features as well:
Detecting and installing IIS in Server 2012R2
The process to detect and install IIS in Server 2012R2 is similar to what we did in Windows 8.1, but it uses slightly different cmdlets. To find out whether IIS is enabled, we will use the Get-WindowsFeature
cmdlet and we will specify Web-*
as the name of the feature we're looking for:
This output is organized very nicely, mimicking the Windows Feature control panel applet. You can easily see the hierarchy of features, and the state of the feature is shown in the box to the left of the display name. In our case, we want to install the Web-Server
feature. We can do this using the Add-WindowsFeature
cmdlet:
Verifying IIS
For either a client or server OS, we would verify that IIS is enabled using the associated Get-
cmdlet, but it makes more sense to me to try to load a web page. IIS configures a default web site with a standard web page, so we should be able to navigate to http://localhost
and know immediately whether everything is working well:
The WebAdministration module
Once IIS is installed, we need to import the WebAdministration
module to interact with IIS (using Import-Module
WebAdministration
). Using the Get-Command
cmdlet to find the Get-
cmdlets in the module is a good way to get an idea about what the module allows us to work with:
Here, we can see that we have cmdlets to deal with sites, virtual directories, applications, bindings, configuration, and much more. Another thing that is worth mentioning at this point is that the WebAdministration module also adds a new PSProvider called WebAdministration and a PSDrive called IIS, which exposes a hierarchical view of the IIS installation. Remember that PSDrives are how PowerShell exposes hierarchical data. In this case, the IIS configuration is treated in a similar way to a drive.
Tip
You try it!
Starting with CD IIS:
, explore the IIS installation using the DIR
(Get-ChildItem
) cmdlet. Don't forget to change back to a filesystem drive when you're done.
Starting, stopping, and restarting IIS
Since you can run command-line programs in PowerShell, the IISReset
command can be used to start, stop, and restart IIS using the /START
, /STOP
, and /RESTART
switches:
If you want to start or stop a particular website rather than the entire IIS installation, you need to use the Start-WebSite
and Stop-WebSite
cmdlets. They both have a –Name
parameter that allows you to specify which site you want to work with. In the following screenshot, I am stopping and starting a website called Test
. Also, I have used the Get-WebSite
cmdlet after each step to show that the Test
site stopped and started correctly:
Creating virtual directories and web applications
Virtual directories and web applications are the different options to contain content in IIS. A virtual directory is a pointer to a location on the disk where the content actually resides. A web application, in the IIS terminology, is a virtual directory that also has the ability to run in a different worker process than its parent.
To create a virtual directory, we will use the New-WebVirtualDirectory
cmdlet and supply the –Name
and –PhysicalPath
parameters. Also, we will need to specify the site and we can do this in one of the following two ways:
- Use
Set-Location
(CD
) in the IIS drive and navigate to the desired site. - Specify the site on the command line.
In the following screenshot, we will illustrate the first method:
If you are working with a website besides the default site (Default Web Site
), you will need to set the location to this site. For instance, to create a virtual directory in the Test site, you would use Set-Location IIS:\Sites\Test\
.
The process to create a web application is similar. The New-WebApplication
cmdlet takes –Name
and –PhysicalPath
parameters as well as a –Site
parameter that can be supplied in the same fashion as with New-WebVirtualDirectory
. Here, we create a new web application called ReportsApp
in the root of the default web site. Note that the cmdlet expects the physical path to be an existing directory, or you can use the –Force
switch to make the cmdlet create the folder for you. I've tried to create a web application with a path that doesn't exist, as shown in the following screenshot. After receiving an error, I retried with –Force
and was successful.
If we wanted the new application to run in a specific application pool, we would have used the –ApplicationPool
parameter to specify its name.
You can easily see the virtual directories and web applications with the IIS drive. After using CD
(Set-Location
) in the \Sites\Default Web Site
folder, DIR
(Get-ChildItem
) shows all of the folders, files, virtual directories, and web applications:
Working with application pools
Given the fantastic cmdlet support for virtual directories and web applications, I was surprised to find that there isn't a Get-WebAppPool
cmdlet. There is a Get-WebAppPoolState
cmdlet, but the formatted output isn't particularly useful.
From the previous screenshot, you can see that there are five application pools and they have all been started, but you don't know what they are called. If one showed Stopped, for instance, you wouldn't know which one you needed to start. Adding Select-Object
–Property
*
helps sometimes, but the values aren't easy to use.
Since the name of the application pool is embedded in an XPath
expression, it is not very easy to work with. Fortunately for us, the application pools are easy to find in the IIS drive, so we can craft our own function to return the app pools.
Creating application pools
We can create an app pool using the New-WebAppPool
cmdlet, which only has one interesting parameter called –Name
. We're going to create an app pool called ReportPool
and later configure the ReportApp
web application to run in this app pool:
Switching an application to run in this pool involves a PSProvider-related cmdlet called Set-ItemProperty
:
We can easily verify that the changes worked using our Get-WebAppPool
function from earlier in the chapter:
Creating application pools
We can create an app pool using the New-WebAppPool
cmdlet, which only has one interesting parameter called –Name
. We're going to create an app pool called ReportPool
and later configure the ReportApp
web application to run in this app pool:
Switching an application to run in this pool involves a PSProvider-related cmdlet called Set-ItemProperty
:
We can easily verify that the changes worked using our Get-WebAppPool
function from earlier in the chapter:
Summary
In this chapter, we saw how to work with IIS, including the installation and validation. We created virtual directories, web applications, and application pools, and learned how to use the IIS drive.
For further reading
Get-Help Get-WindowsOptionalFeature
Get-Help Enable-WindowsOptionalFeature
Get-Help Get-WindowsFeature
Get-Help Add-WindowsFeature
- The WebAdministration module: https://technet.microsoft.com/en-us/library/ee790599.aspx
Get-help Start-Website
Get-help Stop-Website
Get-help Get-Website
Get-help New-WebVirtualDirectory
Get-help New-WebApplication
Get-help Get-WebAppPoolState
Get-help New-WebAppPool
Get-help Set-ItemProperty