How do I enumerate IIS websites using Powershell a

2020-07-03 06:40发布

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.

3条回答
等我变得足够好
2楼-- · 2020-07-03 07:08

Try the following:

[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")

$sm = New-Object Microsoft.Web.Administration.ServerManager

foreach($site in $sm.Sites)
{
    $root = $site.Applications | where { $_.Path -eq "/" }
    Write-Output ("Site: " + $site.Name + " | Pool: " + $root.ApplicationPoolName)
}

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

查看更多
3楼-- · 2020-07-03 07:19

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
}
查看更多
一夜七次
4楼-- · 2020-07-03 07:20

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

$site = Get-IISSite -Name 'my-site'
$appPool = Get-IISAppPool -Name $site.Applications[0].ApplicationPoolName
查看更多
登录 后发表回答