How to monitoring by powershell IIS Worker Processess?

Simon Shone 0 Reputation points
2023-01-14T18:47:57.4233333+00:00

I would really appreciate your help. What should a powershell script look like to get all running IIS Worker Processes and perform an action (show alert) when the number of processes is greater than the value “x”. And is it correct to run it in “do{}while for constant monitoring?

Internet Information Services
{count} votes

2 answers

Sort by: Most helpful
  1. Yurong Dai-MSFT 2,786 Reputation points Microsoft Vendor
    2023-01-15T05:36:10.19+00:00

    Hi @Simon Shone,

    You can try this method to monitor IIS application pool stop/crash and get email alert. Open Event Viewer -> Custom Views -> Server Roles -> Web Server (IIS). This is the log of the web server. The related event log file is saved at C:\Windows\System32\winevt\Logs\System.evtx. When an application pool stops, it creates an event ID 5186 record. You can select a record and choose to attach a task to this event, a wizard will guide you through the process, and then in action, you can send a mail alert.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

    Best regards,

    Yurong Dai

    0 comments No comments

  2. MotoX80 32,076 Reputation points
    2023-01-15T13:24:06.6666667+00:00

    If you have some problem, I think that I would first configure the IIS app pool recycling options and have IIS periodically recycle the worker processes. Next, I would be talking to the site developers and get them to fix whatever is wrong with their code that is causing excess w3wp.exe processes.

    Monitoring is easy, it all depends on your requirements, environment, etc.

    One thing that I used to do was run an HTA on my desktop so that I could monitor my web servers during prime time when an outage would impact many users.

    Here's one example using Powershell.

    $servers = @('test10','webserver2')
    $max = 3                                   # w3wp count limit
    Add-Type -AssemblyName System.speech
    $SpeechSynthesizer = New-Object –TypeName System.Speech.Synthesis.SpeechSynthesizer
    $SpeechSynthesizer.SelectVoice("Microsoft Zira Desktop")
    while ($true) {
        foreach ($server in $servers) {
            [array]$procs = Get-CimInstance win32_process -Filter "Name='w3wp.exe'"  -ComputerName $server
            "{0} - {1} - Count: {2}" -f (get-date), $server, $procs.count 
            If ($procs.count -gt $max) {        # how many? 
                $SpeechSynthesizer.Speak("Warning too many worker processes")   # or some other warning 
            }
        }    
        Start-Sleep -Seconds 60         # how long between tests 
    }
    
    0 comments No comments