Alert NEW IP Address, Send email

Hey folks this is the first time I am using this forum and I am NEW to PowerShell.

Here is the solution I am trying to develop. I would like to create a PowerShell script the will run using Windows scheduler and will run on Windows 10 pro for now. I may have to run the script on servers and other windows versions BUT for now the proof of concept is Windows 10 pro.

The solution needs to check the External (public) IP address of the machine the script runs on. It will then write the results from the IP check process into a text file. If the Text file is not present the script should also create a text file. for Example: MyIPAddress.txt

Not only should the IP address be written to the file but also the date. For example the text file should look like this:

123.873.22.200, 8/23/2020
123.873.22.200, 8/24/2020

ETC… As you can see the IP address is written followed by a comma and then space and then date. This script will run once a day.

The second part of the script now needs to compare the current IP check against the last entry IP address. IF they are different then send an email to the administrator.

Because I haven’t used PowerShell I don’t have the scripts needed BUT I have found a few that could assist. For example here is a script for checking the IP address:

$a = Get-Date

$wc=New-Object net.webclient
$CurrentIP = $wc.downloadstring("") -replace "[^\d\.]"
$row = $CurrentIP + ", " + $a.ToShortDateString() | Out-File C:\IPCheck\my_public_ip.txt -Append

The above script covers half of what I need it to do.

I have another script I found online for sending emails using PowerShell BUT I have not got this to work. Below is the script along with the error message. Ideally, I would like this to be one script to do everything. It may also be nice to have a LOG file generated to capture any errors when the script runs.

Email Script. Note I have removed SMTP Credentials for Gmail Account

$From = ""
$To = ""
$Cc = ""
$Attachment = "C:\IPCheck\my_public_ip.txt"
$Subject = "External IP address"
$Body = "Insert body text here"
$SMTPServer = ""
$SMTPPort = "465"

$cred = ([pscredential]::new('',(ConvertTo-SecureString -String '<my password>' -AsPlainText -Force)))
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred -Attachments $Attachment

Error Message I get

PS C:\IPCheck> C:\IPCheck\SendPSEmail.ps1
Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At C:\IPCheck\SendPSEmail.ps1:11 char:1
+ Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

looks like something related to your smtp server. Are you sure on smtp details, are they correct ?