managing-iis-web-application-pools-in-powershell.jpg

Managing IIS Web Application Pools in PowerShell

Managing IIS Web Application Pools in PowerShell

Need to keep an eye on your IIS web server app pools? Here's a guide on getting started with PowerShell.

Where do you turn when you’re managing an IIS web server farm with potentially dozens or hundreds of app pools? PowerShell scripts, of course! By using the WebAdministration PowerShell module that comes installed as part of IIS and a little PowerShell remoting you can easily create, modify and remove app pools at will. Let’s go through a few examples on how to make that happen.

If you’ve never used PowerShell to manage your IIS servers before your first inclination might be to look for a –ComputerName on most of the cmdlets. Unfortunately, this is not the case. To manage IIS servers remotely, we’re forced to use PowerShell remoting with the Invoke-Command cmdlet. Although not a deal-breaker, it does make the code a little more verbose than what it could be. This is not to put you off managing remote IIS servers with PowerShell but is more of an FYI. I know I was pretty frustrated the first time I tried this and didn’t see that familiar –ComputerName parameter on many of the cmdlets.

NOTE: Going forward, we’ll be building code to input into a scriptblock. We’ll then use Invoke-Command to execute this scriptblock on the remote IIS server.

To manage web application pools, we’ll first need to import the WebAdministration module.

PS> Import-Module WebAdministration

This brings in all of the IIS cmdlets as well as creates the IIS drive. This is where most of the app pool configuration will be done. Let’s first check to see if any app pools already exist.

PS> Get-ChildItem –Path IIS:\AppPools

Name        State       Applications
-----       ------      -------------
GHI         Started
 

It looks like I have one called GHI already. Maybe I want to create another one. Using the IIS drive makes this so easy. Simply use New-Item and specify the path.

PS> New-Item –Path IIS:\AppPools\MyAppPool

Name        State       Applications
-----       ------      -------------
MyAppPool   Started

I’ve now created a new app pool. We can then check all of the properties on that app pool using Get-ItemProperty and select all of the properties it returns with Select-Object. This will return all of the property names and values so you can get figure out which ones you need to modify with Set-ItemProperty.

Get-ItemProperty IIS:\AppPools\MyAppPool | select *

Now that you’ve got an app pool and can see the properties let’s modify a property. Maybe I want to use a particular .NET runtime version with the app pool. Again using the IIS drive I can use Set-ItemProperty to manage app pools like I can the file system, registry, certificates and all the other things that have a PowerShell drive.

PS> Set-ItemProperty -Path IIS:\AppPools\MyAppPool -Name managedRuntimeVersion -Value ‘v4.0’

By using Set-ItemProperty you can modify nearly all of the properties for an app pool.

Finally, we’re done with our app pool and now need to remove it. This time we have a built-in PowerShell cmdlet called Remove-WebAppPool. Simply specify the name, and it’s gone!

Remove-WebAppPool -Name MyAppPool

All of this code we’ve been using was all executed locally but what if you need to run it on a remote IIS server? This is where PowerShell remoting comes in. To do this, we’ll simply need to bundle all of this code up in a scriptblock and then use Invoke-Command to execute it on the remote server.

$appPoolName = ‘MyAppPool’
$scriptBlock = {
    Import-Module WebAdministration
    New-Item –Path IIS:\AppPools\$using:appPoolName
    Set-ItemProperty -Path IIS:\AppPools\$using:appPoolName -Name managedRuntimeVersion -Value ‘v4.0’
    Remove-WebAppPool -Name $using:appPoolName
}

Invoke-Command –ComputerName SOMEIISSERVER –ScriptBlock $scriptBlock

Although not very functional, this code would create a new app pool named MyAppPool, set a property and then remove it. You’ll notice I’m using the $using variable. Since the code in the script block is going to be executing on a remote computer, this is necessary for PowerShell to expand that variable and to use the actual value of `$appPoolName` that was declared locally on our client computer.

If you’d like to learn more about managing IIS in general check out the Technet IIS Administration page. There you’ll find all of the cmdlets included in the WebAdministration module and how you can use them.

 

Related Posts


Comments
Comments are disabled in preview mode.
Loading animation