Monday, 21 January 2019

Powershell Script to export a list of all proxyaddresses for all users in the O365 tenant



This script allows you to export a list of all users in the tenant and all associated proxy addresses.  It’s helpful to be able to get this as a csv filer for future reference so that you can quickly filter on whichever user you like.  It’s also been helpful for me on a recent migration where I was able to make sure that each user’s email addresses were properly migrated.




$LogFilePath = $env:LOCALAPPDATA + "\Cloudwyse\Logs\user_proxy_addresses_" + $(get-date -Format ddMMyy_HHmmss) + ".log"

Start-Transcript -Path $LogFilePath -NoClobber

$365Pass = cat C:\cloudwyse\securestring365.txt | convertto-securestring

$365Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$365Pass

$DateTime = (Get-Date -Format "ddMMyyyy-HHmmss")


$365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $365cred -Authentication Basic -AllowRedirection

Import-PSSession $365Session

Connect-Msolservice -Credential $365Cred



Write-Host  -ForegroundColor Magenta "Pulling mailbox information for all users, please be patient..."

$JobStart = Get-Date

$getmailbox = get-Mailbox

$JobEnd = Get-Date

$JobSecondsTaken =($JobEnd - $JobStart)

Write-Host  -ForegroundColor Yellow "Extract complete.  The Job took" $JobSecondsTaken.Seconds "seconds."

$total = $null

$Job2Start = Get-Date

$userList = @()

foreach ($user in $getmailbox)   {

$lookup = get-msoluser -userprincipalname $user.userprincipalname

       Write-Host  -ForegroundColor Magenta "Current user is" $user.userprincipalname

       $addresses = $lookup.proxyaddresses

       foreach ($address in $addresses) {

       $us = New-Object PSObject

             $us | Add-Member -type NoteProperty -Name 'UPN' -Value $lookup.userprincipalname

             $us | Add-Member -type NoteProperty -Name 'ProxyAddresses' -Value $address

             Write-Host  -ForegroundColor Cyan "Address is" $address

             $userList += $us


       $total = $total +1



$Job2End = Get-Date

$Job2SecondsTaken =($Job2End - $Job2Start)

Write-Host -ForegroundColor Yellow "$total users processed in" $Job2SecondsTaken.Minutes "minute(s) and" $Job2SecondsTaken.Seconds "second(s)."

Remove-PSSession $365Session

$userlist | export-csv C:\Cloudwyse\user_proxy_addresses$datetime.csv

Write-Host -ForegroundColor Yellow "Report exported to C:\Cloudwyse\user_proxy_addresses$datetime.csv"




I’ve shared this before, but if you are unsure how to securely store credentials in a script without using plain text then follow the instructions here to create the securestring.txt.




No comments:

Post a Comment