question

AnirbanBanejee-4620 avatar image
0 Votes"
AnirbanBanejee-4620 asked ·

Difference between Get-WebAppPoolState & Get-WebsiteState

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 :(




windows-server-powershellwindows-server-iis-general
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered ·

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



capture.jpg (70.3 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnirbanBanejee-4620 avatar image
0 Votes"
AnirbanBanejee-4620 answered ·

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


image.png (32.7 KiB)
image.png (44.0 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered ·

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 
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnirbanBanejee-4620 avatar image
0 Votes"
AnirbanBanejee-4620 answered ·

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



image.png (8.7 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just assign to a variable.

 $count = $ApplicationSiteState.add(@{($_.itemxpath -split ("'"))[1]="$($_.value)"})

Tell the operations team to go work on some trouble tickets.

I think the difference is that since the first method is using a pipeline, PS knows that there is only one entry and there is no need for a key. But with a .add, PS doesn't know how many more entries there will be and has to assume that keys will be needed. That's a semi-educated guess on my part.

1 Vote 1 ·