PowerShell:检索AppPool中的应用程序数量

问题描述 投票:7回答:4

如何通过PowerShell命令检索与特定IIS AppPool关联的应用程序数?

我们可以手动查看相关的应用程序:

Get-Item IIS:\AppPools\AppPoolName

但是,如果我们手动想要选择Applications列,则无法实现。此外,Applications列未在| Get-Member *中列出。

  1. 为什么列未列出?
  2. 如何使用PowerShell查找与特定IIS AppPool关联的应用程序数?
iis powershell application-pool
4个回答
15
投票

诀窍是:PowerShell建立了所谓的“视图定义文件”,它告诉PowerShell如何格式化对象(例如,对象是否被格式化为列表或表格,显示哪些列等)。这些文件可以在C:\Windows\System32\WindowsPowerShell\v1.0找到,并且都以.format.ps1xml结尾。

回答原始问题:文件C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\iisprovider.format.ps1xml包含AppPool类型的视图定义,它定义了一个如下所示的计算列:

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

这就解释了为什么列本身不是AppPool类型的成员。现在可以从上面的“scriptlet”中提取必要的代码,轻松回答第二个问题:

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

3
投票

我处理了同样的问题好几个小时,直到最终达成解决方案。 D.R.的答案非常有帮助,但它不适合我。经过一些调整后,我想出了下面的代码,它可以检索应用程序池中的应用程序数量。

我注意到这部分代码nd @path!='/'丢掉了计数。

$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

2
投票

我最终得到了以下代码(基本上与上面相同,但格式不同)

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

0
投票

这给出了你在数组中看到的东西。

导入模块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;
© www.soinside.com 2019 - 2024. All rights reserved.