PowerShell: retrieve number of applications in AppPool

Viewed 8402

How to retrieve the number of applications associated with a specific IIS AppPool via PowerShell command?

We can see the associated applications manually using:

Get-Item IIS:\AppPools\AppPoolName

However, if we manually want to select the Applications column, it is not possible. Also, the Applications column is not listed within | Get-Member *.

  1. Why is the column not listed?
  2. How to find the number of applications associated with a specific IIS AppPool using PowerShell?
5 Answers

I ended up with the following Code (basically the same as above, but differently formatted)

$appPools = Get-ChildItem –Path IIS:\AppPools
foreach ($apppool in $apppools) {
  $appoolName = $apppool.Name
  [string] $NumberOfApplications = (Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool='$appoolName']" "machine/webroot/apphost" -name path).Count
  Write-Output "AppPool name: $appoolName has $NumberOfApplications applications"
}

I recently came across this post searching for ways to get the active Application Pools. The information provided above was great, but I kept digging to see if there was another way get this information. I was able to find a way to do this through Get-IISSite, which I used the following:

Get-IISSite | Select-Object -ExpandProperty Applications | Select-Object Path,ApplicationPoolName

I tested this on a server that only had one website, but if there are multiple sites on the server, you could also add VirtualDirectories for the Select.

I also had a need to just get a unique list of the Application Pools being used, so I did the following:

$appPoolInfo = Get-IISSite | Select-Object -ExpandProperty Applications | Select-Object Path,ApplicationPoolName

$appPoolInfo | Select-Object -Unique ApplicationPoolName

This gives what you are looking in an array.

Import-Module WebAdministration;

Get-ChildItem IIS:\AppPools >> AppPoolDetails.txt;
$appPoolDetails = Get-Content .\AppPoolDetails.txt;
$w = ($appPoolDetails |Select-String 'State').ToString().IndexOf("State");
$w = $w -1;
$res1 = $appPoolDetails | Foreach {
    $i=0;
    $c=0; `
    while($i+$w -lt $_.length -and $c++ -lt 1) {
        $_.Substring($i,$w);$i=$i+$w-1}}
Write-Host "First Column---";   
$res1.Trim();
$j = $w + 1;    
$w = ($appPoolDetails |Select-String 'Applications').ToString().IndexOf("Applications");
$w = $w -$j;
$res2 = $appPoolDetails | Foreach {
    $i=$j;
    $c=0; `
    while($i+$w -lt $_.length -and $c++ -lt 1) {
        $_.Substring($i,$w);$i=$i+$w-1}}
Write-Host "Second Column---";  
$res2.Trim();
$lineLength=0
$appPoolDetails | Foreach {
    if($lineLength -lt $_.TrimEnd().Length )
    {
        $lineLength = $_.TrimEnd().Length;
        #Write-Host $lineLength;
    }
}

$j = ($appPoolDetails | Select-String 'Applications').ToString().IndexOf("Applications");
$w = $lineLength;
$w = $w -$j;
#Write-Host $j $w;
$res3 = $appPoolDetails | Foreach {
    $i=$j;
    $c=0; `
    while($i+$w -lt $_.length -and $c++ -lt 1) {
        $_.Substring($i,$w);$i=$i+$w-1}}
Write-Host "Third Column---";   
$res3;
Related