AsiaTech: Microsoft APGC Internet Developer Support Team

We focus on various troubleshooting plan and solution on IIS web platform and distributed applications

PowerShell scripts to monitor web server crash event\hang and send notification emails

PowerShell scripts to monitor web server crash event\hang and send notification emails

Rate This
  • Comments 1

Web application administration team often requires to have a quick way to monitor web application performance and running status. After studying some articles (thanks to Dmitry’s sharing) , I'd like to share this Powershell script to do this job efficiently, you may use it a sample code and extend its functions you like (such as dump collection):

 

IMPORTANT: Before running the script, should ensure the Powershell policy allow this. The command in Powershell will be:

 

Set-ExecutionPolicy RemoteSigned

 

For details of this command, refer to: http://technet.microsoft.com/en-us/library/ee176961.aspx

 

A. For Hang pattern, this script can be running on client or server locally, save it as monitorweb.ps1, then run it. This script will monitor the web URL every 30 seconds, if there is no responding or exception occurs, one email can be sent out to your email box.

================================

 function MonitorSite {

      param($URL)

                         $emailFrom = "your email address@test.com"

                        $emailTo = "your email address@test.com "

                        $smtpServer = "your smtp server"

                        $smtp = new-object Net.Mail.SmtpClient($smtpServer)

                        $smtp.Credentials = New-Object System.Net.NetworkCredential("your email account of the smtp server", "your password of the smtp server");

     trap{

        "Failed. Details: $($_.Exception)"

                        $subject = "Exception Sent from PowerShell"

                        $body = "Fail`t`t" + "`t`t"+"Failed. Details: $($_.Exception)"

                        $smtp.Send($emailFrom, $emailTo, $subject, $body)

        exit 1

    }

    $webclient = New-Object Net.WebClient

   #some sites may refuse your test if the user agent is not explicit

   # $webClient.Headers.Add("user-agent", "IE")

    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials

    if($webclient.Proxy -ne $null)     {

        $webclient.Proxy.Credentials = `

                [System.Net.CredentialCache]::DefaultNetworkCredentials

    }

 while (1 -eq 1)

{

    $startTime = get-date

    $webclient.DownloadString($URL) | Out-Null

     $endTime = get-date

 #should not slower than 120 seconds

 if (($endTime - $startTime).TotalSeconds -ge 120)

{

 "       TimeOut. "

                        $subject = "Timeout Sent from PowerShell"

                        $body = "Timeout`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"

                        $smtp.Send($emailFrom, $emailTo, $subject, $body)

}

else

{

"Success`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"

 }

#ping site every 30 seconds

sleep(30)

}

 }

  MonitorSite  "http://mytestsite"

==================

 

B.  To detect typical event failure, for example, 5011 of WAS warning , we can follow below action on Win2008:

Save this sample code as sendmail.ps1

==============================================================

$emailFrom = "your email address@test.com"

$emailTo = "your email address@test.com "

$subject = "Sent from PowerShell"

$body = "Triggered by Event"

$smtpServer = "your smtp server"

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Credentials = New-Object System.Net.NetworkCredential("your email account of the smtp server", "your password of the smtp server");

$smtp.Send($emailFrom, $emailTo, $subject, $body)

==========================================================================

 

Open a command line, run:

 

SCHTASKS /Create /TN EventLog /TR "Powershell.exe -NonInteractive -command H:\powershell\ sendmail.ps1" /SC ONEVENT /EC System /MO *[System/EventID=5011] /RU "SYSTEM"

 

This will create a task to monitor Event 5011, and drop email to admin when it occurs.

 

Regards,

Freist Li from APGC DSI Team

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • I unable to work by SharePoint, Are exist an algorithm or note for Study and knowing better in my Language Persian or no?

Page 1 of 1 (1 items)