PowerShell: retrieve number of applications in AppPool

守給你的承諾、 提交于 2019-12-12 08:40:06

问题


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?

回答1:


The trick is: PowerShell established so-called "view definition files" which tell PowerShell how to format objects (e.g. whether the object is formatted as a a list or a table, which columns are displayed, etc.). Those files can be found at C:\Windows\System32\WindowsPowerShell\v1.0 and are all ending in .format.ps1xml.

To answer the original question: The file C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\iisprovider.format.ps1xml contains the view definition for the AppPool type which defines a calculated column looking like this:

<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>

This answers why the column itself is not a member of the AppPool type. The second question can be easily answered now extracting the necessary code from the "scriptlet" above:

$applicationsInAppPoolCount = @(Get-WebConfigurationProperty `"/system.applicationHost/sites/site/application[@applicationPool=`'$appPool`'and @path!='/']"` "machine/webroot/apphost" -name path).Count



回答2:


I dealt with this same issue for many hours until finally arriving at the solution. The answer from D.R. was very helpful but it was not working for me. After some tweaks, I came up with the code below which retrieves the number of applications in an app pool.

I noticed that this part of the code nd @path!='/' threw off the count.

$appPool = "REPLACE ME with a value from your app pool"
@(Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool=`'$appPool`']" "machine/webroot/apphost" -name path).Count



回答3:


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"
}



回答4:


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;


来源:https://stackoverflow.com/questions/20636916/powershell-retrieve-number-of-applications-in-apppool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!