Sorry I’m not home right now
Walking into IIS Webs
Just leave a message and I’ll call you back – No Doubt, if they were techies
So today we’re going to look into the IIS provider in PowerShell. If you aren’t familiar with PowerShell providers, check out Matt’s post on PowerShell providers. If you’re dealing with IIS 7.0, you’ll want to look into the IIS PowerShell snap-in. Otherwise, if you’re dealing with IIS 7.5, you’ll want to look into the IIS PowerShell module. Today’s post will be looking at IIS 7.5 and the WebAdministration module.
First things, first!
In order to work with the IIS module, you need to start a PowerShell session with elevated permissions. If you don’t, then you’ll get an error like this:
PS C:\users\sdutkiewicz:> Import-Module WebAdministration
Process should have elevated status to access IIS configuration data.
Once you start PowerShell as an administrator, you can use Get-Module to see if the WebAdministration module is loaded. If it isn’t loaded, then run:
The module is loaded. What kind of goodies are in the IIS module?
A lot of IIS functionality that you’re used to in the GUI is also available in the WebAdministration module, packed into a variety of cmdlets. There are cmdlets to help with:
- web configuration
- web applications
- application pools
- web bindings
To see a full list of cmdlets, aliases, and functions included in the WebAdministration module, run the following command:
Get-Command -Module WebAdministration
You mentioned an IIS provider…
Yes, I did! If you run the following command, you’ll see a list of providers installed:
You should see a provider named WebAdministration, which includes an IIS drive. Let’s see what it allows us to explore.
If we run a Get-ChildItem at this point, we’ll see that the IIS provider lets us navigate through 3 parts of IIS – AppPools, Sites, and SslBindings. Today, we’ll look at Sites, as they are the central point. The Sites folder in the IIS provider contains websites, web applications, and virtual directories. From the IIS: prompt, run the following command:
If you run the Get-ChildItem command at this point, you should see the list of sites setup on your web server. It may look like the picture below:
You’ll notice that my sadukievents site shows a state of being stopped. We can easily fix this using one of the cmdlets included in the WebAdministration module:
Notice how simple it was – no need to remember an IIS metabase path! Now let’s say that same site was getting carried away with the resources and we needed to stop the site. The Stop-Website cmdlet works similarly. Of course, sites rarely misbehave at an appropriate time. When you open PowerShell as an administrator, you’re dropped at your system folder (unless you’ve altered that). Rather than changing to the IIS drive and stopping the site (whose name you already know), you can run this command from your current drive with the following command:
Now back to IIS:\Sites… what do you see once you change directories into a Site? When you run Get-ChildItem (or its built-in alias of dir) on a site, it shows you the contents of the site. The image below shows what’s in the sadukievents website:
You can see that the output from this particular folder looks like a directory listing – showing the file mode, last write time, length, and name. This appears this way because these are all files or directories. What if there was a virtual directory or possibly a web application in this folder? Then the output changes to show the type (application, directory, file, or virtualDirectory), name, and physical path. You can see a sample of this below:
At this point, you should feel comfortable navigating through the Sites folder. You can use Get-ChildItem and Set-Location to navigate through the AppPools and SslBindings folders in the same way, learning more about the AppPools and SslBindings.
So that’s navigation. Can we do anything else in here?
Besides navigating the IIS drive, you can also use it much like you would other PowerShell provider drives. When working with IIS cmdlets from outside of the IIS drive, you can reference the IIS drive as a path. For example, let’s say you’re working in a clustered web environment due to high-availability requirements and need to setup a new website on all the servers in the cluster. Sure, you could do it manually on each server, but why waste your time when you can script it? Here’s a sample command for creating a new web application:
New-Item ‘IIS:\Sites\Sadukieville’ -Type Application -PhysicalPath d:\Websites\Sadukieville
If that example web application had a virtual directory for a bakery site within it, it may be scripted like this:
New-Item ‘IIS:\Sites\Sadukieville\Bakery’ -Type VirtualDirectory -PhysicalPath c:\inetpub\wwwRoot\sadukieria
Both of the above examples show how you can use the IIS provider as a path with other cmdlets. Of course, there’s plenty more that you can do. For more information, explore the cmdlets as suggested above, and know that Get-Help and Get-Help command -examples are at your fingertips to guide you through how those cmdlets work and how you can take advantage of the IIS provider while scripting your web administration tasks.
Do you have specific questions about PowerShell that you want answered? Whether it’s IT pro related or developer related, I can probably help! Drop me a line at sarah at codinggeekette dot com, and I’ll try to cover your questions in an upcoming blog post.
Matt and I love feedback from our readers, so please email us if you have comments or suggestions.