Search code examples
powershelliiswmi

How do I enumerate IIS websites using Powershell and find the app pool for each?


I can search for websites using:

Get-WmiObject -Namespace "root\WebAdministration" -Class Site -Authentication PacketPrivacy -ComputerName $servers

And I can list the app pools using:

Get-WmiObject -computer $servers -Namespace root\MicrosoftIISv2 -Class IIsApplicationPoolSetting -Impersonation Impersonate -Authentication PacketPrivacy

How can I link these together to find which app pool is associated to which website? It's a Windows Server 2008 R2 server with IIS 7.5.


Solution

  • Try the following:

    [Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
    $sm = New-Object Microsoft.Web.Administration.ServerManager
    foreach($site in $sm.Sites) {
        foreach ($app in $site.Applications) {
            [PSCustomObject]@{
                Application = $site.Name + $app.Path
                Pool = $app.ApplicationPoolName
            }
        }
    }
    

    The script above lists every site on the server and prints the root application pool name for each site.