如何通过PowerShell命令检索与特定IIS AppPool关联的应用程序数?
我们可以手动查看相关的应用程序:
Get-Item IIS:\AppPools\AppPoolName
但是,如果我们手动想要选择Applications
列,则无法实现。此外,Applications
列未在| Get-Member *
中列出。
诀窍是: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
我处理了同样的问题好几个小时,直到最终达成解决方案。 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
我最终得到了以下代码(基本上与上面相同,但格式不同)
$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"
}
这给出了你在数组中看到的东西。
导入模块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;