Skip to content

Instantly share code, notes, and snippets.

@win2000b
Last active April 27, 2017 14:27
Show Gist options
  • Save win2000b/cbccfbf56be3f71f46fe88b60d635388 to your computer and use it in GitHub Desktop.
Save win2000b/cbccfbf56be3f71f46fe88b60d635388 to your computer and use it in GitHub Desktop.
Enabled Litigation Hold and Auditing for all mailboxes where it doesn't exist.
#To generate the encrypted password file, login as the user in which the task will run under.
$credential = Get-Credential schedule365@company.co.uk
$credential.Password | ConvertFrom-SecureString | Set-Content "Schedule365.txt"
#365 Admin Account to be used
$admin = "schedule365@company.co.uk"
#Get Encrypted Password File
$password = Get-Content "C:\Scripts\Schedule365.txt" | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $admin,$password
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic –AllowRedirection -ErrorAction Stop
Import-PSSession $exchangeSession -ErrorAction Stop
#Get a list of all users and Shared mailboxes that have E3 licenses applied but with Litigation Hold NOT enabled
$Users = Get-Mailbox -RecipientTypeDetails UserMailbox -Filter {PersistedCapabilities -eq "BPOS_S_Enterprise" -and LitigationHoldEnabled -ne $true}
$Shared = Get-Mailbox -RecipientTypeDetails SharedMailbox -Filter {PersistedCapabilities -eq "BPOS_S_Enterprise" -and LitigationHoldEnabled -ne $true}
#Get a list of all users and Shared mailboxes that have Auditing NOT Enabled
$UserAudit = Get-Mailbox -RecipientTypeDetails UserMailbox -Filter {AuditEnabled -ne $true}
$SharedAudit = Get-Mailbox -RecipientTypeDetails SharedMailbox -Filter {AuditEnabled -ne $true}
#For each user returned above enable Litigation hold
foreach ($User in $Users) {
Set-Mailbox -Identity $User.UserPrincipalName -LitigationHoldEnabled $true -ea silentlycontinue
}
#For each Shared Mailbox returned above enable Litigation hold
foreach ($User in $Shared) {
Set-Mailbox -Identity $User.UserPrincipalName -LitigationHoldEnabled $true -ea silentlycontinue
}
#For each user returned above enable Auditing
foreach ($User in $UserAudit) {
Set-Mailbox -Identity $User.UserPrincipalName -AuditEnabled $true -AuditLogAgeLimit 365 -AuditOwner Create,HardDelete,MailboxLogin,MoveToDeletedItems,SoftDelete,Update -ea silentlycontinue
}
#For each Shared Mailbox returned above enable Auditing
foreach ($User in $SharedAudit) {
Set-Mailbox -Identity $User.UserPrincipalName -AuditEnabled $true -AuditLogAgeLimit 365 -AuditOwner Create,HardDelete,MailboxLogin,MoveToDeletedItems,SoftDelete,Update -ea silentlycontinue
}
Remove-PSSession $exchangeSession
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment