Last active
April 8, 2019 18:25
-
-
Save nickadam/fb71158fc7fcb1ba726c3491feace52d to your computer and use it in GitHub Desktop.
Get all users forwardto or redirectto inbox rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# evidently there is an entirely different area in o365 where you can do the same thing as an inbox rule but it's not an inbox rule | |
# Get-Mailbox -ResultSize Unlimited | select WindowsEmailAddress,AccountDisabled,DeliverToMailboxAndForward,ForwardingAddress,ForwardingSmtpAddress | Export-Csv -NoTypeInformation Forward.csv | |
# get msol creds for reconnecting | |
$creds = Get-Credential | |
# First get a list of all users from on prem AD so it doesn't time out | |
Get-ADUser -Properties emailaddress -Filter {(Enabled -eq $True) -and (ProxyAddresses -like 'SMTP*')} | Export-Csv -NoTypeInformation Users.csv | |
# write the header | |
[PSCustomObject]@{ | |
'EmailAddress' = ''; | |
'RuleName' = ''; | |
'ForwardTo' = ''; | |
'RedirectTo' = ''; | |
} | ConvertTo-Csv -NoTypeInformation | Select -First 1 | Out-File -Encoding 'Default' -Append Rules.csv; | |
$n = 0 | |
Import-Csv Users.csv | ForEach { | |
$User = $_ | |
if($User.EmailAddress){ | |
# connect to exchange | |
if($n -eq 0){ | |
((Get-Date).toString("yyyy-MM-dd HH:mm:ss") + " Connecting to Remote Exchange") | |
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell" -Credential $creds -Authentication Basic -AllowRedirection | |
Import-PSSession $Session | |
} | |
((Get-Date).toString("yyyy-MM-dd HH:mm:ss") + " Working on " + $User.EmailAddress) | |
$Rules = Get-InboxRule -Mailbox $User.EmailAddress | Where {($_.Enabled -eq $True) -and (($_.ForwardTo -ne $null) -or ($_.RedirectTo -ne $null))} | |
if(($Rules | measure).count -gt 0){ | |
$Rules | ForEach { | |
((Get-Date).toString("yyyy-MM-dd HH:mm:ss") + " Writing rule " + $Rule.Name + " for " + $User.EmailAddress) | |
$Rule = $_ | |
[PSCustomObject]@{ | |
'EmailAddress' = $User.EmailAddress; | |
'RuleName' = $Rule.Name; | |
'ForwardTo' = $Rule.ForwardTo; | |
'RedirectTo' = $Rule.RedirectTo; | |
} | ConvertTo-Csv -NoTypeInformation | Select -Last 1 | Out-File -Encoding 'Default' -Append Rules.csv; | |
} | |
} | |
$n = ($n + 1) | |
# reconnect to exchange every 100 users | |
if($n -eq 100){ | |
$n = 0 | |
Remove-PSSession $Session | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment