Office 365 – Backup all smtp address to csv

1. Before do this create batch file with this content:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\ProgramData\Scripts\365-BCKP\365-BCKP.ps1""' -Verb RunAs}"

2. Save it to backup.bat
3. Create ps1 with this content:

$AdminUsername = "[email protected]"
$AdminPassword = "Your_365_admin_Password"
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminUsername,$SecurePassword
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection 
Import-PSSession $Session -AllowClobber
Get-mailbox | select DisplayName,RecipientType,EmailAddresses | Export-Csv C:\ProgramData\Scripts\365-BCKP\365-Mailbox.csv
Get-contact | select displayname,RecipientType,WindowsEmailAddress | Export-Csv C:\ProgramData\Scripts\365-BCKP\365-Contacts.csv
Get-DistributionGroup | select displayname,RecipientType,emailaddresses | Export-Csv C:\ProgramData\Scripts\365-BCKP\365-Groups.csv
Remove-PSSession $Session
wscript "C:\Scripts\365-BCKP\365-BCKP-notification.vbs"

Create 365 admin account only for this job.

4. Create vbs script to mail notification!

Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.1.1" 'your smtp server domain or IP address goes here
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.From = "[email protected]"
objMail.To = "[email protected]"
objMail.Subject = "365 Beckup"
objMail.TextBody = "365 backup was created"
objMail.AddAttachment "C:\Scripts\365-BCKP\365-BCKP.csv"
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing