Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Get-OrphanedMailbox {
$ExSplat = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = 'http://exchange.example.com/powershell'
Authentication = 'Kerberos'
}
$ExSession = New-PSSession @ExSplat
Import-PSSession -Session $ExSession *>$null
$SharedMailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox
foreach ($SharedMailbox in $SharedMailboxes) {
$Permissions = $SharedMailbox | Get-MailboxPermission | Where-Object {
$_.IsInherited -eq $false -and $_.User -like 'EXMAPLE\*' -and $_.User -ne 'EXAMPLE\serviceaccount'
}
if ($null -eq $Permissions) {
$CalendarIdentitiy = '{0}:\Calendar' -f $SharedMailbox.PrimarySmtpAddress
$CalendarPermissions = Get-MailboxFolderPermission -Identity $CalendarIdentitiy | Where-Object {
$_.User -notin @('Default', 'Anonymous') -and $_.User -like 'EXAMPLE\*'
}
if ($null -eq $CalendarPermissions) {
[PSCustomObject]@{
Mailbox = $SharedMailbox.Name
SMTPAddress = $SharedMailbox.PrimarySmtpAddress
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment