简体   繁体   中英

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.

Use the webadministration module:

Import-Module WebAdministration

dir IIS:\Sites # Lists all sites
dir IIS:\AppPools # Lists all app pools and applications


# List all sites, applications and appPools

dir IIS:\Sites | ForEach-Object {

    # Web site name
    $_.Name

    # Site's app pool
    $_.applicationPool

    # Any web applications on the site + their app pools
    Get-WebApplication -Site $_.Name
}

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.

Here's another option using IISAdministration if you do not want to use the IIS:\ path.

Import-Module IISAdministration

$site = Get-IISSite -Name 'my-site'
$appPool = Get-IISAppPool -Name $site.Applications[0].ApplicationPoolName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM