This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

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?

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

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

Reply
  • 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.

Children
  • Hi All,

    Common application pools like ‘MSExchangeOWAAppPool’ or ‘MSExchangeECPAppPool’ are commonly hijacked by attackers through web shell deployment.

    Is there any powershell command to know when IIS webserver AppPool process executing net.exe, cmd.exe, and mshta.exe ?