/
Email Encryption Rule-All Customers.ps1
24 lines (18 loc) · 1.23 KB
/
Email Encryption Rule-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 = "Email Encryption"
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
$EmailEncryptRule = Get-TransportRule | Where-Object {$_.Identity -contains $TransportRuleName}
if (!$EmailEncryptRule) {
Write-Output "Rule for Encryption not found, creating Rule"
New-TransportRule -Name "Encrypt Email" -SubjectContainsWords "Secure" -ApplyRightsProtectionTemplate "Encrypt"
}
Remove-PSSession $session
}