Finding unused app pools with powershell

I’ve been tasked with cleaning up our UAT environment, and needed a way to find unused App Pools. To make things more exciting, we don’t have Microsoft.Web.Administration.ServerManager installed, so the native powershell way wasn’t going to work. This is my solution, by invoking appcmd and string parsing the results.

$command = "c:\windows\system32\inetsrv\appcmd.exe list app"
$apps = Invoke-Expression $command

$command = "c:\windows\system32\inetsrv\appcmd.exe list apppool"
$pools = Invoke-Expression $command
foreach ($pool in $pools)
{
    $used = $false
    foreach ($app in $apps)
    {
        $apppool = $app
        $apppool = $apppool.Substring($apppool.indexof(":")+1)
        $apppool = $apppool.Replace(")","")
        $apppool = $apppool.trim()
        if ($pool.contains($apppool))
        {
            $used = $true
            break
        }

    }
    echo "$used $pool"
}
Advertisements

About robertlabrie
DevOps Engineer at The Network Inc in metro Atlanta. Too many interests to list here, check out my posts, or look me up on LinkedIn

2 Responses to Finding unused app pools with powershell

  1. this is an excellent script but i did bit modification to the script so that i can run it over multiple servers but its not returning more than 5 apppool details eventhough server’s are having more than 10 apppools .any idea why?

    here is the modified script

    $ServerName = Get-Content “C:\Karthik\Serverslist1.txt” -ErrorAction Stop
    foreach ($Server in $ServerName)
    {

    $command = “c:\windows\system32\inetsrv\appcmd.exe list app”
    $apps = Invoke-Expression $command

    $command = “c:\windows\system32\inetsrv\appcmd.exe list apppool”
    $pools = Invoke-Expression $command
    foreach ($pool in $pools)
    {
    $used = $false
    foreach ($app in $apps)
    {
    $apppool = $app
    $apppool = $apppool.Substring($apppool.indexof(“:”)+1)
    $apppool = $apppool.Replace(“)”,””)
    $apppool = $apppool.trim()
    if ($pool.contains($apppool))
    {
    $used = $true
    break
    }

    }
    echo “$server $used $pool”
    }
    }

  2. Never mind. i got it now 🙂

    $ServerName = Get-Content “C:\Karthik\Serverslist1.txt”

    foreach ($Server in $ServerName)
    {
    $apps = Invoke-Command -Credential $cred -ComputerName $Server -ScriptBlock {C:\Windows\System32\inetsrv\appcmd.exe list app}

    $pools = Invoke-Command -Credential $cred -ComputerName $Server -ScriptBlock {C:\Windows\System32\inetsrv\appcmd.exe list apppool}

    foreach ($pool in $pools)
    {
    $used = $false
    foreach ($app in $apps)
    {
    $apppool = $app
    $apppool = $apppool.Substring($apppool.indexof(“:”)+1)
    $apppool = $apppool.Replace(“)”,””)
    $apppool = $apppool.trim()

    if ($pool.contains($apppool))
    {
    $used = $true

    }
    }
    echo “$Server ; $used ; $pool” >> C:\Karthik\AppPool.csv

    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: