Difference between Get-WebAppPoolState & Get-WebsiteState

Anirban Banejee 1 Reputation point
2021-03-09T12:22:41.163+00:00

Hello I was trying to write a 2 Powershell Script to be able to capture All AppPoolState and All WebSiteState for IIS. But I am stumped with this

[System.Collections.ArrayList]$ApplicationPoolsState = Get-WebAppPoolState | % { return @{($.itemxpath -split ("'"))[1]="$($.value)" } }

$ApplicationPoolsState.keys .NET v4.5 .NET v4.5 Classic DefaultAppPool

But

[System.Collections.ArrayList]$ApplicationSiteState = Get-WebsiteState | % { return @{($.itemxpath -split ("'"))[1]="$($.value)" } }

$ApplicationSiteState.keys

The Second one returns blank. Even Though the array is there > $ApplicationSiteState Name Value ---- ----- Default Web Site Started

What am I missing :(

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,150 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,359 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-03-09T12:59:05.857+00:00

    Seems to be working for me. Please use the Code Sample tool when posting code. Otherwise the site removes underscore characters.

    cls  
    [System.Collections.ArrayList]$ApplicationSiteState = Get-WebsiteState | % {   
        return @{  
            ($_.itemxpath -split ("'"))[1]="$($_.value)"  
        }   
     }  
     "===========Site State ==========="  
     $ApplicationSiteState | Format-Table  
      
     [System.Collections.ArrayList]$ApplicationPoolsState = Get-WebAppPoolState | % {   
        return @{  
            ($_.itemxpath -split ("'"))[1]="$($_.value)"   
        }   
     }  
    "===========Pool State ==========="   
    $ApplicationPoolsState | Format-Table  
    

    75840-capture.jpg

    0 comments No comments

  2. Anirban Banejee 1 Reputation point
    2021-03-09T17:35:55.323+00:00

    Hello

    Thank You very much for your response

    For ApplicationSiteState see the .keys is blank

    75829-image.png

    While in ApplicationPoolState

    75929-image.png

    My Full Code - it is forked from https://github.com/pszafer/check_iis_pool

    I made necessary modification to be able to exclude few AppPool which doesn't need monitoring
    I similarly trying to do it for WebSite which doesn't need monitoring

    #################################################################################  
    #  
    # NAME:  check_iis_site.ps1  
    # USAGE: check_iis_site.ps1 -ES 'Default Web Site'  [ES is array to exclude site]  
    #  
    # COMMENT:  Script to check for IIS Pool with Nagios/Icinga2 + NRPE/NSClient++  
    #  
    #           Checks:  
    #           - if there is any app which is in stopped state  
    #  
    # Return Values for NRPE:  
    # Everything started - OK (0)  
    # There is some App with stopped state - CRITICAL (2)  
    # Script errors - UNKNOWN (3)  
    #  
    # NRPE Handler to use with NSClient++:  
    # [NRPE Handlers]  
    # check_updates=cmd /c echo scripts\check_iis_pool.ps1 $ARG1$ $ARG2$; exit $LastExitCode | powershell.exe -command -   
    #  
    #  
    # CHANGELOG:  
    # 0.1  2017-08-23 - initial version  
    #  
    #################################################################################  
    # Copyright (C) 2017 Pawel Szafer pszafer@gmail.com  
    #  
    # This program is free software; you can redistribute it and/or modify it under   
    # the terms of the GNU General Public License as published by the Free Software   
    # Foundation; either version 3 of the License, or (at your option) any later   
    # version.  
    #  
    # This program is distributed in the hope that it will be useful, but WITHOUT   
    # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS   
    # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.  
    #  
    # You should have received a copy of the GNU General Public License along with   
    # this program; if not, see <http://www.gnu.org/licenses>.  
    #################################################################################  
      
    param([String[]]$ES)  
    Import-Module WebAdministration  
      
      
    $returnStateOK = 0  
    # $returnStateWarning = 1  
    $returnStateCritical = 2  
    $returnStateUnknown = 3  
      
    $statuses = @{  
     ok = @()  
     critical = @()  
    }  
      
    $criticalTitles = "";  
    $countCritical = 0;  
    $countOK = 0;  
    if (-Not ($ParamAvailable = $PSBoundParameters.ContainsKey('ES'))){  
        $ApplicationSiteState = Get-WebsiteState | % {  return  @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator | % {  
         if ($_.value -ne "Started"){  
         $statuses.critical += $_.key  
         }  
         else{  
         $statuses.ok += $_.key  
         }  
        }  
    }  
    else{  
        [System.Collections.ArrayList]$ApplicationSiteState = Get-WebsiteState | % {  return  @{($_.itemxpath -split ("'"))[1]="$($_.value)" } }  
        write-host $ApplicationSiteState.keys  
        foreach($h in $ES){  
            if ($($ApplicationSiteState.keys).IndexOf($h) -ge 0){  
                $ApplicationSiteState.RemoveAt($($ApplicationSiteState.keys).IndexOf($h))  
            }  
        }  
        $ApplicationSiteState | % getEnumerator | % {  
         if ($_.value -ne "Started"){  
         $statuses.critical += $_.key  
         }  
         else{  
         $statuses.ok += $_.key  
         }  
        }  
    }  
      
    $countCritical = $statuses.critical.length  
    $countOK = $statuses.ok.length  
      
    if ($countCritical -gt 0) {  
     if ($countOK -gt 0){  
     Write-Host "CRITICAL - Sites:" + $statuses.critical + " but some Sites are OK:" + $statuses.ok  
     exit $returnStateCritical  
     }  
     else {  
     Write-Host "CRITICAL - Sites:" + ($statuses.critical | String-Out) + " "  
     exit $returnStateCritical  
     }  
      
     # report critical, report OK  
    }  
    elseif ($countOK -gt 0){  
     Write-Host "OK - all Sites are ok " + ($statuses.ok -join ',')  
     exit $returnStateOK  
     # report OK  
    }  
    else {  
     Write-Host "Cannot check Site state"  
     exit $returnStateUnknown  
    }  
    

    We have a problem in around line number 70

    0 comments No comments

  3. MotoX80 31,571 Reputation points
    2021-03-09T22:34:13.063+00:00

    Appears to be related to the fact that you only have one web site, and thus one entry in the arraylist.

    I played around and this appears to work. Give it a try.

     [System.Collections.ArrayList]$ApplicationSiteState = @() 
     Get-WebsiteState | % { 
         $ApplicationSiteState.add(@{($_.itemxpath -split ("'"))[1]="$($_.value)"})
     }
     "===========Site State ==========="
     $ApplicationSiteState | Format-Table
     "===========Keys ==========="
     $ApplicationSiteState.keys 
      
    
    0 comments No comments

  4. Anirban Banejee 1 Reputation point
    2021-03-10T04:02:30.423+00:00

    Hello

    Thank You very much. Why will a single item array list not work (not sure)

    With this method

    [System.Collections.ArrayList]$ApplicationSiteState = @()   
        Get-WebsiteState | % {   
          $ApplicationSiteState.add(@{($_.itemxpath -split ("'"))[1]="$($_.value)"})  
        }  
    

    It is working OK - but I am getting output of

    0
    1
    2
    Etc (count of number of website). Is it possible to stop that print ?
    If there is an issue sending this count down will irate the operations team :)

    76141-image.png