-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate Remote Mailbox.ps1
More file actions
48 lines (38 loc) · 1.88 KB
/
Create Remote Mailbox.ps1
File metadata and controls
48 lines (38 loc) · 1.88 KB
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
48
<#
Create missing remote mailboxes in local exchange.
Author: Patrick Duckles
Version: 1.0
#>
# Setup Credentials for connecting to remote services
$UPN = <EMAiL>
$O365CREDS = Get-Credential -message "Enter Office 365 Credentials... (Your UPN)" -UserName $UPN
$ONPREMCREDS = Get-Credential -message "Enter Local Record Exchange Admin... (Domain Admin)" -UserName "<EXCHANGE_ADMIN>"
# Create and connect to remote session xchng-ol
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365CREDS -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
# Check for MSOnline module. If not found, install.
If(-not(Get-InstalledModule MSOnline -ErrorAction silentlycontinue)){
Install-Module MSOnline -Confirm:$False -Force
}
Import-Module MSOnline
Connect-MsolService -credential $O365CREDS
# Get list of mailboxes present on o365 in the UK.
$data = get-mailbox -Filter {UsageLocation -eq "United Kingdom"}
# Close down xchng-ol session.
Get-PSSession | Remove-PSSession
# Connect to local exhcnage server.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri <URL OF EXCHANGESERVER> -Authentication Kerberos -Credential $ONPREMCREDS
Import-PSSession $Session -DisableNameChecking
# Run through each user. If they dont exsist as a remote mailbox in exchange, Create. Otherwise move on.
foreach($user in $data) {
$error.Clear()
$name = $user.Name.ToString()
$name = $name.replace("."," ")
Get-RemoteMailbox -Identity $name | out-null
if ($error -ne $null){
$RRA = $user.Alias + "@DOMAIN.LOCAL"
Enable-RemoteMailbox $user.Name -RemoteRoutingAddress $RRA
Set-RemoteMailbox $user.Name -ExchangeGuid $user.ExchangeGuid
}}
Get-PSSession | Remove-PSSession
if (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}