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

D.R*_*.R. 7 iis powershell application-pool

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

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

Get-Item IIS:\AppPools\AppPoolName
Run Code Online (Sandbox Code Playgroud)

但是,如果我们手动想要选择Applications列,则无法进行.此外,该Applications列未列在其中| Get-Member *.

  1. 为什么列未列出?
  2. 如何使用PowerShell查找与特定IIS AppPool关联的应用程序数?

D.R*_*.R. 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>
Run Code Online (Sandbox Code Playgroud)

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

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


Nik*_*kin 5

我处理了同样的问题很多小时,直到最终找到解决方案。DR 的回答非常有帮助,但对我来说不起作用。经过一些调整后,我想出了下面的代码,它检索应用程序池中的应用程序数量。

我注意到这部分代码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
Run Code Online (Sandbox Code Playgroud)