-
Notifications
You must be signed in to change notification settings - Fork 14
/
Block Auto-FW_All Customers.ps1
24 lines (19 loc) · 1.53 KB
/
Block Auto-FW_All Customers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$credential = Get-Credential
Connect-MsolService -Credential $credential
$customers = Get-MsolPartnerContract -All
$TransportRuleName = "Block Auto-Forwarding"
$rejectMessage = "To improve security, auto-forwarding rules to external email addresses have been disabled. Please contact your helpdesk if you want to create an exception"
Write-Output "Found $($customers.Count) customers for $((Get-MsolCompanyInformation).displayname)."
foreach ($customer in $customers) {
$InitialDomain = Get-MsolDomain -TenantId $customer.TenantId | Where-Object {$_.IsInitial -eq $true}
Write-Output "Checking transport rule for $($Customer.Name)"
$DelegatedOrgURL = "https://outlook.office365.com/powershell-liveid?DelegatedOrg=" + $InitialDomain.Name
$session = New-PSSession -ConnectionUri $DelegatedOrgURL -Credential $credential -Authentication Basic -ConfigurationName Microsoft.Exchange -AllowRedirection
Import-PSSession $session -CommandName Get-TransportRule, New-TransportRule, Set-TransportRule -AllowClobber
$externalForwardRule = Get-TransportRule | Where-Object {$_.Identity -contains $TransportRuleName}
if (!$externalForwardRule) {
Write-Output "Rule for Auto-forwarding not found, creating Rule"
New-TransportRule -name "Block Auto-forwarding" -Priority 1 -SentToScope NotInOrganization -FromScope InOrganization -MessageTypeMatches AutoForward -RejectMessageEnhancedStatusCode 5.7.1 -RejectMessageReasonText $rejectMessage
}
Remove-PSSession $session
}