11 Replies Latest reply on Dec 13, 2019 2:40 PM by stephend

    IIS App Pool Status with Powershell

    t0ta11ed74

      I realize AppInsight can do this out of the box, but for various reasons AppInsight isn't an option. I'm trying to get the status of an application pool and display it using a Powershell monitor, but having an issue with getting it to work right;

       

      import-module WebAdministration

      $results = Get-ChildItem IIS:\AppPools\* | Where-Object { $_.Name -like "www.mysite.com*" }

       

      foreach ($item in $results) {

       

      $appPool = $item.Name

      $status = $item.State

       

      write-host "Message:$appPool"

      write-host "Statistic.:$status"

       

       

      }

       

      The above only lists the first result, and shows Statistic as "NaN".  If I remove the Statistic line, it outputs all of the results, but reports "get output failed".

       

      What am I missing here?

        • Re: IIS App Pool Status with Powershell
          t0ta11ed74

          I guess no one knows the answer to this one. I'm still working on getting a monitor to do this....

            • Re: IIS App Pool Status with Powershell
              justthwackit

              Hey there,

              I just randomly found your post and thought I would help you out, even if I am a bit late.

               

              I had some trouble with the output as well but once you understand it it becomes obvious where the mistake was in your script.

               

              This is what I created from your script:

               

              $results = $item = $appPool = $status = $NULL

              $status = "1"

              import-module WebAdministration

              $results = Get-ChildItem IIS:\AppPools\* #| Where-Object { $_.Name -like "www.mysite.com*" }

               

              foreach ($item in $results) {

               

              $appPool += "`n"

              $appPool += $item.Name + " - state: " + $item.State

              if ($item.State -ne "Started") {$status = "0"}

              #$status = $item.State

              }

               

              write-host "Message:$appPool"

              write-host "Statistic.:$status"

               

               

              It's not perfect but it does the job. Little explanation: the script will list all your websites together with the state in the message variable and it outputs a statistic that is either "1" if all your websites are running or "0" if one of the websites is down.

              If you want it to be more specific, you can put all these variables in different variables (up to 10 pairs of message and statistic variables I think). You can not put out the output in a loop, you have to collect the data and output it at the very end of the script.

               

              In your case you probably know how many websites are running on the server so you could do it without a loop. Just go through the array and fill your different message and statistics variables.

               

              I am by no means an expert and this is not a perfect solution, but I thought a nudge in the right direction might help you. If you have any questions, feel free to ask. I will do my best to answer them.

              1 of 1 people found this helpful
            • Re: IIS App Pool Status with Powershell
              t0ta11ed74

              Thanks man! I'll give that try when I get a chance.

              • Re: IIS App Pool Status with Powershell
                t0ta11ed74

                This works in that it shows all the pools and their state, but when testing the script output, Orion returns "Get output failed". When testing the monitor, it fails with "Unknown status".

                • Re: IIS App Pool Status with Powershell
                  t0ta11ed74

                  Since I'm only interested in whether the app pools are running or not, this works with SAM interpreting a "Down" or "Up" state with the exit codes;

                   

                  import-module WebAdministration

                  $status = 0

                  $results = Get-ChildItem IIS:\AppPools\* | Where-Object { $_.Name -like "*power*" }

                   

                  foreach ($item in $results) {

                  if ($item.State -eq "Stopped") {

                  $status = 1

                  write-host "Statistic:$status"

                  exit 1;

                  }

                  }

                  write-host "Statistic:$status"

                  exit 0;

                  • Re: IIS App Pool Status with Powershell
                    cscoengineer

                    Got it to work and made some tweaks.   There are two components:  AppPools gives the name of AppPool and its State.   AppPoolsStopped gives the name of the stopped AppPool and its state.

                     

                    Make sure you run this on x64 mode.    I have upload this template to content exchange.

                    AppPools:

                    Import-Module WebAdministration

                    $status=0

                    $AppPool=""

                    $AppPoolStopped=""

                    $AppPoolStoppedCount=0

                    $results = Get-ChildItem IIS:\AppPools\*

                    foreach ($item in $results) {

                       $AppPool=$AppPool+$item.name+"("+$item.state+"), "

                       if ($item.state -eq "Stopped") {$AppPoolStopped=$AppPoolStopped+"("+$item.state+"), " ; $AppPoolStoppedCount=$AppPoolStoppedCount+1}

                       }

                    $AppPool=$AppPool.substring(0,$AppPool.length-2)

                    Write-Host "Message:  $AppPool";

                    Write-Host "Statistic: 1"

                    #Write-Host "Message: $AppPoolStopped"

                    #if ($AppPoolStopped -eq "") {

                    #Write-Host "Statistic: 0"; exit 0} else {Write-Host "Statistic: $AppPoolStoppedCount"; exit 1}

                     

                     

                    AppPoolsStopped:

                    Import-Module WebAdministration

                    $status=0

                    $AppPool=""

                    $AppPoolStopped=""

                    $AppPoolStoppedCount=0

                    $results = Get-ChildItem IIS:\AppPools\*

                    foreach ($item in $results) {

                       $AppPool=$AppPool+$item.name+"("+$item.state+"), ";

                       if ($item.state -eq "Stopped") {$AppPoolStopped=$AppPoolStopped+$item.name+"("+$item.state+"), " ;

                            $AppPoolStoppedCount=$AppPoolStoppedCount+1

                       }

                       }

                    #Write-Host "Message:  $AppPool";

                    #Write-Host "Statistic: 1"

                    $AppPoolStopped=$AppPoolStopped.substring(0,$AppPoolStopped.length-2)

                    Write-Host "Message: $AppPoolStopped"

                    if ($AppPoolStopped -eq "")

                         {Write-Host "Statistic: 0"; exit 0}

                        else {Write-Host "Statistic: $AppPoolStoppedCount"; exit 0}

                     

                    Couldn't get the exit 1 to work.  There are a few threads on it.