4

When looking in the IIS 7.5 manager > application pools, The last column lists "Applications". This column shows the number of application pools / websites this appPool is associated with.

I am trying to figure out how to query this column / information using Powershell. The end goal here is to have a script that I could run that would tell me if any applicaiton pool is being used for more than 1 website or app.

I am unable to find how to query this information, when running:

get-itemproperty IIS:\AppPools\(AppPoolName) | format-list *

I dont see this property. Im not sure that this column is a property, if not, is there a best way to check if AppPools are being used for more than 1 website / applicaiton?

colo_joe
  • 119
  • 1
  • 5
  • 12
  • Also to Add... when running the command get-itemproperty IIS:\AppPools\(AppPoolName) Again I see the Applicaitons column, but I cannot find how to query just that information. Even when saving the info to a variable and piping to get-member, I dont see a property to get the Applications column information. – colo_joe May 10 '12 at 18:46

3 Answers3

4

The Applications property is defined in the format file, its code reside in the iisprovider.format.ps1xml file (in the webadmin module folder).

        <TableColumnItem>
          <ScriptBlock>
            $pn = $_.Name
            $sites = get-webconfigurationproperty "/system.applicationHost/sites/site/application[@applicationPool=`'$pn`'and @path='/']/parent::*" machine/webroot/apphost -name name
            $apps = get-webconfigurationproperty "/system.applicationHost/sites/site/application[@applicationPool=`'$pn`'and @path!='/']" machine/webroot/apphost -name path
            $arr = @()
            if ($sites -ne $null) {$arr += $sites}
            if ($apps -ne $null) {$arr += $apps}
            if ($arr.Length -gt 0) {
              $out = ""
              foreach ($s in $arr) {$out += $s.Value + "`n"}
              $out.Substring(0, $out.Length - 1)
            }
          </ScriptBlock>
        </TableColumnItem>

You can take the code out and use it outside the format file, just assign $pn the apppool name you want to query. Here's a simplified version of the code:

$pn = 'pool1'
$sites = get-webconfigurationproperty "/system.applicationHost/sites/site/application[@applicationPool='$pn' and @path='/']/parent::*" machine/webroot/apphost -name name
$apps = get-webconfigurationproperty "/system.applicationHost/sites/site/application[@applicationPool='$pn' and @path!='/']" machine/webroot/apphost -name path
$sites,$apps | foreach {$_.value}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Wish I would have seen your post earlier, this is what I eventually came up with: (sorry for the formatting, limited space) $SiteApps = get-item IIS:\Sites\* $arraySize = ($SiteApps.count -1) $i = 0 $ t = 0 for ($i=0; $i -le $arraySize; $i ++) # start at the beg of the array{ for ($t=($i+1); $t -le $arraySize; $t++) {if ($siteApps[$i].applicationpool -eq $siteApps[$t].applicationpool) {$web1 = $siteApps[$i].name $webappPool = $siteApps[$i].applicationpool $web2 = $siteApps[$t].name $answer = $answer + "The website "$web1" is sharing the AppPool "webAppPool" with website "$web2". " }}} – colo_joe May 11 '12 at 14:14
1

I went with this:

Import-Module WebAdministration

function Get-WebAppPoolApplications($webAppPoolName) {
    $result = @()

    $webAppPool = Get-Item ( Join-Path 'IIS:\AppPools' $webAppPoolName )
    if ( $webAppPool -ne $null ) {
        $webSites = Get-ChildItem 'IIS:\Sites'
        $webSites | % {
            $webApplications = Get-ChildItem ( Join-Path 'IIS:\Sites' $_.Name ) |
                where { $_.NodeType -eq 'application' }

            $result += $webApplications |
                where { $_.applicationPool -eq $webAppPoolName }
        }
    }

    $result
}
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
0

Wish I would have seen your post earlier, this is what I eventually came up with:

$SiteApps = get-item IIS:\Sites* $arraySize = ($SiteApps.count -1) 
$i = 0 
$t = 0 
for ($i=0; $i -le $arraySize; $i ++) # start at the beg of the array
{ 
for ($t=($i+1); $t -le $arraySize; $t++) 
{
if ($siteApps[$i].applicationpool -eq $siteApps[$t].applicationpool) 
{
$web1 = $siteApps[$i].name 
$webappPool = $siteApps[$i].applicationpool 
$web2 = $siteApps[$t].name $answer = $answer + "The website "$web1" is sharing the AppPool "webAppPool" with website "$web2". " 
}
}
}
colo_joe
  • 119
  • 1
  • 5
  • 12