0
votes

I have created script for checking if VPN connection is disconnected and try to reconnect. Its also sending emails, when 1) cant ping VPN server with remote LAN address 2) VPN connection fails 3) VPN connection is established.

First email is sent and received properly, but Powershell gives error, when it have to send another email.

Code inside vpn.ps1 file:

$ip = "*.*.*.*"
$result = Get-WmiObject -Query "SELECT * FROM Win32_PingStatus WHERE Address = '$ip'"

if($result.StatusCode -eq 0) {

} else {
    Start-Sleep -s 15

    $EmailFrom = "*@*.*"
    $EmailTo = "*@*.*" 
    $Subject = "###" 
    $Body = "#####################################################"

    $SMTPServer = "*.*.*.*" 
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, ***)
    #$SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("*@*.*", "*************");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

    rasdial.exe ********** /DISCONNECT    
    rasdial.exe ******** ****** ******

    Start-Sleep -Seconds 15

    $ip = "*.*.*.*"
    $result = Get-WmiObject -Query "SELECT * FROM Win32_PingStatus WHERE Address = '$ip'"

    if ($result.StatusCode -eq 0) {
        $EmailFrom = "*@*.*"
        $EmailTo = "*@*.*" 
        $Subject = "########" 
        $Body = "####################################" 
        $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    } else {
        Start-Sleep -s 15

        $EmailFrom = "*@*.*"
        $EmailTo = "*@*.*" 
        $Subject = "################" 
        $Body = "##########################################" 
        $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    }
}

Log when running:

PS Microsoft.PowerShell.Core\FileSystem::***********> *******\vpn.ps1
#FIRST EMAIL SENT
No connections
Command completed successfully.
Connecting to *********...
Verifying username and password...
Registering your computer on the network...
Successfully connected to ******.
Command completed successfully.
Exception calling "Send" with "4" argument(s): "Failure sending mail."
At \\tsclient\C\New folder\vpn.ps1:26 char:9
+         $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException


PS Microsoft.PowerShell.Core\FileSystem::******

I have sensored usernames, passwords, addresses etc. What is wrong with my script?

1
"Failing sending mail." is a general error message, usually indicates failure in finding or connecting to the $SmtpServer - Mathias R. Jessen
But the first email is sent properly. - user6401704
Have you tried to pause for a few minutes (start with 1, then 5) before sending another mail ? Maybe there is a protection against spam on the SMTP server ? - sodawillow

1 Answers

0
votes

Your $SMTPClient object isnt declared in the else block. You need an instance of that object inside of the block, where you use it, or declare it outside, to have one global $SMTPClient object, which is present in the blocks. You also have to fill the $SMTPClient.Credentials.

Best solution will be, to declare both before the if Statement in line 3.

Update

Move these lines

$SMTPServer = "*.*.*.*" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, ***) 
#$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("*@*.*", "*************"); 

out of those if .. else ..

Just move it to line 2, right before the if block.