Using a ready script Get-Office365Endpoints.ps1 connect to https://endpoints.office.com.
Filter that information with IPV4 and SMTP, then send it as txt-file thru Exchange Online using smtp.office365.com
If the endpoint txt-file is found script is stopped. Also includes Aes-encryption for secure password storing.
Only Change EmailAddress, everything else can be used for all environments. You can make a scheduled task of this or run it manually.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# If You want to send addresses, remove # from next line # rm $env:temp\O365_endpoints_Worldwide_latestversion.txt $FileLocation = "$env:temp" $CheckFile = Test-Path -path $filelocation\O365_endpoints_Worldwide_latestversion.txt If ($checkfile -Eq $True) {Write-Host "Exchange Online SMTP endpoints are up-to-date"} Else{ # Define the Get-Office365Endpoint function in the current PowerShell session Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/janegilring/PSCommunity/master/Office%20365/Get-Office365Endpoint.ps1')) # Retrieve endpoints for Exchange Online with filter TCP port 25 and IPV4 only $ExchangeOnlineEndpoints = Get-Office365Endpoint -Services Exchange -Output IPv4 $ExchangeOnlineSMTPEndpoints = $ExchangeOnlineEndpoints | Where-Object { $PSItem.ip -and $PSItem.DisplayName -eq 'Exchange Online' -and $PSItem.tcpPorts -contains '25' } # Variable for temp-txt file $ExchangeOnlineSMTPEndpoints | select ip > $env:temp\eop_ip.txt $ip = "$env:temp\eop_ip.txt" # Credentials from encrypted file. If You don't see file: # # Create Aes-encryption # # $Key = New-Object Byte[] 32 # [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key) # $Key | out-file aes.key # # Create Encrypted file with Aes-encryption # # (get-credential).Password | ConvertFrom-SecureString -key (get-content aes.key) | set-content "password.txt" $EmailAddress = "whateveruser@somedomain.fi" $password = Get-Content password.txt | ConvertTo-SecureString -Key (Get-Content aes.key) $Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $EmailAddress, $Password Send-MailMessage -SmtpServer smtp.office365.com -usessl -Port 587 -credential $cred -From $EmailAddress -To $EmailAddress -Subject "New Exchange Online SMTP ip-addresses" -Body "New Exchange Online SMTP ip-addresses" -Attachments $ip } |