PowerShell:检索AppPool中的应用程序数量 [英] PowerShell: retrieve number of applications in AppPool

查看:185
本文介绍了PowerShell:检索AppPool中的应用程序数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,如果我们手动想要选择 Applications 列,则无法进行。此外,列中未列出应用程序列Get-Member *

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. 为什么列未列出?

  2. 如何使用PowerShell查找与特定IIS AppPool关联的应用程序数量?


推荐答案

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

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.

回答原始问题:文件 C:\ Windows \ System32 \ WindowsPowerShell \ v1.0\Modules\WebAdministration\iisprovider.format.ps1xml 包含 AppPool 类型的视图定义,该类型定义了一个计算列查找像这样:

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>

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

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

这篇关于PowerShell:检索AppPool中的应用程序数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆