基于 Powershell 中的输入数组删除某些 AppPool

Remove Certain AppPool based on input Array in Powershell

我正在尝试显示 IIS 的 AppPool 是否已启动。 默认情况下,它将显示所有 AppPool 状态。

但是,如果在 运行 运行脚本时传递参数 -EA (--exceptionAppPool),它将不会考虑那些 AppPool 的状态。

当 -EA 被传递时,我以某种方式卡在了 else{} 语句中。不确定如何从 Get-WebAppPoolState 方法中考虑删除 -EA 中传递的数组列表

param([String[]]$EA)
if (-Not ($ParamAvailable = $PSBoundParameters.ContainsKey('EA'))){
    $ApplicationPoolsState = Get-WebAppPoolState | % {  return  @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator | % {
        if ($_.value -ne "Started"){
            $statuses.critical += $_.key
        }
        else{
            $statuses.ok += $_.key
        }
    }
}
else{
      #----I want to exclude the Keys which is present in -EA array ?
      #----The example of -EA is .NET v4.5,.NET v4.5 Classic,DefaultAppPool
      $ApplicationPoolsState = Get-WebAppPoolState | % {  return  @{($_.itemxpath -split ("'"))[1]="$($_.value)" } }
      for ($i=0; $i -lt $EA.length; $i++){
          $ApplicationPoolsState = $ApplicationPoolsState.Remove($EA[$i])
      }
      $ApplicationPoolsState = $ApplicationPoolsState | % getEnumerator | % {
      if ($_.value -ne "Started"){  
           $statuses.critical += $_.key
      }
      else{
        $statuses.ok += $_.key
    }
}
 

脚本将 运行 喜欢 .\check_appPool.ps1 -EA .NET v4.5,.NET v4.5 Classic,DefaultAppPool

遇到错误

Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."

如何在 else 语句中排除那些不被检查的对象

从固定大小数组中删除对象的一种方法是创建一个新数组,其中仅包含从原始数组中选择的对象。这篇博客中有解决此类问题的方法,可以参考it.