Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Wednesday, 11 December 2019

Powershell - compare two mailboxes in hash table and export to CSV

 

I thought I’d share a useful little powershell trick I used today which allowed me to easily compare two very similar mailboxes in Office 365.  I had a user who had a duplicate mailbox which had been inadvertently created through an issue with AAD Sync.  I knew all the fields I would be comparing would be identical so I used a hash table with embedded arrays to compare the information I was interested in. 

 

First create a variable for each mailbox which will contain all the information we will be comparing.  Then create the hashtable.

 

 

$user1 =  get-mailbox -identity user.name@contoso.com

$user2 =  get-mailbox -identity user.name@contoso-corp.com

$combined = @{}

 

Next pipe the fields from the variable into the hash table, creating an array for each one.  Then pipe the variables containing our mailbox data into the nested arrays in the hashtable.

 

 

$user1.psobject.properties | Foreach { $combined[$_.Name] = @() }

$user1.psobject.properties | Foreach { $combined[$_.Name] += $_.Value }

$user2.psobject.properties | Foreach { $combined[$_.Name] += $_.Value }

 

 

Finally I converted the hashtable back to a PSObject so that I could export it to a csv file and analyse the output in Excel.

 

$combined.getenumerator() | ForEach-Object {

    New-Object -Type PSObject -Property @   {

        'Field' = $_.Name

        'contoso.com' = $_.Value[0]

        'contoso-corp.com' = $_.Value[1]

                                            }

    } | Select-Object Field, contoso.com, contoso-corp.com | Export-Csv C:\cloudwyse\comparison.csv -NoType

   

 

The whole (short) script is available in the Gist below.

 

Friday, 8 February 2019

Powershell LDAP query to find Azure / O365 users synchronised with AD Sync

 

 

Recently I needed to create a quick report that would allow me to see at a glance which accounts in that domain had been synchronised with AD Sync into Azure AD.  It wasn’t possible using Get-ADuser and I knew an LDAP query would do the trick.  First I had to download a powershell module called System.DirectoryServices.Protocols.  Once the module is downloaded run:

 

 

Add-Type -AssemblyName System.DirectoryServices.Protocols

Import-Module C:\Cloudwyse\Tools\S.DS.P.psm1

 

Then to query the information I required I ran:

 

 

$MigratedUsers=Find-LdapObject -SearchFilter:"(msDS-ExternalDirectoryObjectId=*)" -SearchBase:"DC=contoso,DC=com" -LdapConnection:"server01.contoso.com" -PageSize 500

 

 

Conversely, if you wanted to find all users that HADN’T been synchronised you could run the following:

 

 

$MigratedUsers=Find-LdapObject -SearchFilter:"(!msDS-ExternalDirectoryObjectId=*)" -SearchBase:"DC=contoso,DC=com" -LdapConnection:"server01.contoso.com" -PageSize 500

 

 

I still had a few service accounts showing so I just filtered these in Excel based on the DN.  To export the fil just run…

 

 

Export-CSV C:\Cloudwyse\User_report.csv

 

 

 

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 "admin@contoso.com",$365Pass

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

 

$365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -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"

 

Stop-Transcript

 

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.