Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Add timeout to this script?

$
0
0

Hi.

Im using a script to check availability of a url. It works well, but I would like to have a timeout in the script.

The script checks the site ever 15 mins and I want the script to sent out email if the site is down 10 secs.

The script:

function Test-Site {
    param($URL)
    trap{
        "Failed. Details: $($_.Exception)"
        $emailFrom = "pech@my-site.dk"
        # Use commas for multiple addresses
        $emailTo = "pech@my-site.dk,clan@my-site.dk,josc@my-site.dk,dane@my-site.dk,grpitsupport@my-site.dk,GrpMarketingKommunikationsteam@my-site.dk"
        $subject = "my-site.dk nede"
        $body = "my-site.dk webside er nede!. Details: $($_.Exception)"
        $smtpServer = "10.1.4.30"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
        exit 1
    }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
} 
Test-Site "http://my-site.dk"
Best regards

Viewing all articles
Browse latest Browse all 2562

Trending Articles