MENU
Business.com aims to help business owners make informed decisions to support and grow their companies. We research and recommend products and services suitable for various business types, investing thousands of hours each year in this process.
As a business, we need to generate revenue to sustain our content. We have financial relationships with some companies we cover, earning commissions when readers purchase from our partners or share information about their needs. These relationships do not dictate our advice and recommendations. Our editorial team independently evaluates and recommends products and services based on their research and expertise. Learn more about our process and partners here.
Where do you turn when managing an IIS web server farm with potentially dozens or hundreds of app pools? PowerShell, of course.
Microsoft PowerShell is the favored tool of many information technology (IT) experts who manage Internet Information Services (IIS) web server farms with dozens or hundreds of app pools.
You can create, modify and remove app pools using the WebAdministration PowerShell module that comes as part of the IIS package and the tool’s remote management capabilities.
If you’ve never used PowerShell to manage your IIS servers, 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, you must use PowerShell remoting with the Invoke-Command cmdlet.
Although not a deal-breaker, using the Invoke-Command cmdlet makes the code a little more verbose. It can be pretty frustrating the first time you try this because you won’t see that familiar –ComputerName parameter on many of the cmdlets.
Note: Going forward, we’ll build code to input into a scriptblock. We’ll then use Invoke-Command to execute this scriptblock on the remote IIS server.
To manage application pools, we must first import the WebAdministration module.
Import-Module WebAdministration
The WebAdministration module imports all ISS cmdlets and creates the IIS drive. That’s where most (or all) of the app pool configuration happens. It’s similar to how syncing folders with PowerShell organizes your files in a streamlined way.
Now, we’ll check to see if any app pools already exist:
Get-ChildItem –Path IIS:AppPools
It looks like we have one called GHI already, so making another one makes sense.
The IIS drive makes creating a new app pool easy. It’s like manipulating file system ACLs using PowerShell. To do both, you use New-Item and specify the path:
New-Item –Path IIS:AppPoolsMyAppPool
We’ve now created a new app pool.
Next, we’ll check all the properties on that app pool using Get-ItemProperty and select all the properties it returns with Select-Object. This will return all property names and values to help us determine which ones we must modify with Set-ItemProperty. With this command, we can make each app pool to our exact specifications.
Get-ItemProperty IIS:AppPoolsMyAppPool | select *
Now that we have an app pool and can see its properties, let’s modify a property. Maybe you want to use a specific .NET runtime version with the app pool. Again, using the IIS drive, you can use Set-ItemProperty to manage app pools as you would with the file system, registry, certificates and other items with a PowerShell drive:
Set-ItemProperty -Path IIS:AppPoolsMyAppPool -Name managedRuntimeVersion -Value ‘v4.0’
By using Set-ItemProperty, we can modify nearly all app pool properties.
Now that we’re done with the app pool, we must remove it. We have a built-in PowerShell cmdlet called Remove-WebAppPool. Specify the name and it’s gone.
Remove-WebAppPool -Name MyAppPool
We’ve been executing code locally up until now. However, what if we need our code to run on a remote IIS server? Remoting with loops helps us do this. We must bundle the code in a scriptblock and 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
This code would create a new app pool named MyAppPool, set a property, then remove it. This is a great example of how functions in PowerShell allow you to perform multiple tasks with one set of commands.
You’ll notice we’re using the $using variable. Since the code in the script block will execute on a remote computer, this is necessary for PowerShell to expand that variable and use the actual value of $appPoolName that was declared locally on your client computer.
Mark Fairlie contributed to this article.