cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 12

IIS App Pool Status with Powershell

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?

0 Kudos
11 Replies
Level 12

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.

pastedImage_0.png

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.

Level 7

Thx for the template....however how do i check the status of multiple app pool names I have running on my IIS servers. I have more app pools than the defaultapppool and I need to know when they go down or crash? Thank

0 Kudos
Level 7

Actually let me rephrase my question....how do i create an alert rule to trigger when a app pool is down? thx

0 Kudos
Level 12

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;

0 Kudos
Level 12

Hi.  I can't seem to get it to work - I get the following error:

pastedImage_0.png

Any ideas?

0 Kudos
Level 12

This usually means wherever you're trying to import the module, it doesn't exist.

0 Kudos
Level 12

It works when I run it in PowerShell window, but doesn't inside SAM.   Does the module have to be copied into SAM somewhere?

0 Kudos
Level 12

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".

0 Kudos
Level 12

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

0 Kudos
Level 12

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

0 Kudos
Level 9

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.

0 Kudos