Site performance monitoring by powershell script

Hi,

Here I have given the powershell script to check for the site availability and also to monitor the site performance. In both case, it will trigger mail to the configured users or groups.




if ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.Powershell 
}

#----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#######################################################################################################################################################################
##########################Script to find the avalability and performance of the site###################################################################################
#######################################################################################################################################################################

####To monitor if the site availability and performance and to triggers mail #####################


################Declaring a global variable var1###################################################
[double] $global:var1 = ""
[double] $global:var1
clear-host #### Hides the variable $global:var1
################function to ping the site or list of sites#########################################
function Ping-Url {

    param(
        [Parameter(ValueFromPipeline=$true)][string] $url
    )
    process {

        $request = [System.Net.WebRequest]::Create( $url )
        $request.UseDefaultCredentials = $true
        $request.Timeout = 9000000;
        $startTime = get-date -displayhint time
        $request.GetResponse() > $null
        $stopTime = get-date -displayhint time
 $duration = $stopTime-$startTime
# $duration | out-file <<c:\Testscript.txt>>

 $global:var1=  [double]$duration.totalmilliseconds
 write-host "Measured Response duration value of the site $x is " $global:var1 "ms"

    }


##############Assigning a constant value for a benchmark duration for comparing it with the response duration##########
$iDuration = new-object system.timespan 0,0,0,0,3130
$var2 = [double]$iduration.totalmilliseconds
write-host "Benchmark duration value for good performance : " $var2 "ms"
####To perform the test for list of sites, kindly seperate the URL by commas as shown
$array = @("<<site link with comma delimiter for more sites>>")
foreach ($x in $array)
{
#############Validating the status(down/up) of the site###############################################################
##############If the site is down it gets trapped here and the exception will be sent as an mail to the administrator##
trap{
        "Failed. Details: $($_.Exception)"
        $emailFrom = "<<mail id>>"
        # Use commas for multiple addresses
        $emailTo = "<<Mail id>>"
        $subject = "<<Some subject>>"
        $body = "Team , `n The Application - $x  is not available. Please check for the reason. `n `n The details are: $($_.Exception) `n "
        $smtpServer = "<<SMTP server name>>"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
    }
##############Pinging the site#########################################################################
##############For Loop starts to ping the sites for three times########################################################

for( $i = 1 ; $i -le 3 ; $i++)
{
##############Calling the function#####################################################################################
$x | Ping-Url
##############Subtracting the response time from the site ($global:var1) with the benchmarkduration(var2)##############
$sub = $global:var1-$var2
write-host "value obtained by subtracting benchmark duration and the response duration : " $sub "ms"
}
##############Comparing the variables########################################################################################
 if($global:var1 -lt $var2)
 {
 continue       
     }
 else
 {
 $emailFrom = "<<Mail id>>"
        # Use commas for multiple addresses
        $emailTo = "<<Mail id>>"
        $subject = "<<Some subject>>"
        $body = "Team , `n The Application -  $x is taking little bit time to load. Please check for the reason. `n "
        $smtpServer = "<<SMTP server name>>"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
       
     }

}
##############################################################################################################################
#clear-host

Comments

Popular posts from this blog

Run Powershell script on Remote Computer/Server

Power BI Refresh Issue: unable to convert the value '' from the source data type 'VT_BSTR' to the expected data type 'VT_I8.

SPDActivities - DP.SharePoint.Workflow - Workflow failed to run