Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinblumenfeld/f573b36db55d3535669dc58070ca36e8 to your computer and use it in GitHub Desktop.
Save kevinblumenfeld/f573b36db55d3535669dc58070ca36e8 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $false)]
[switch] $WithoutInPlaceHold,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string[]] $list
)
Begin {
$resultArray = @()
$findParameter = "InPlaceHolds"
$mailboxProperties = @("displayname", "userprincipalname", "IsInactiveMailbox", "accountdisabled", "RecipientTypeDetails", "inplaceholds")
$mbxSearch = Get-MailboxSearch -ResultSize unlimited | select name, inplaceholdidentity, Status, version, StartDate, EndDate, sourcemailboxes, ItemHoldPeriod
$hash = @{}
foreach ($sRow in $mbxSearch) {
foreach ($id in $sRow.InPlaceHoldIdentity) {
$hash[$id] = $sRow
}
}
}
Process {
if ($WithoutInPlaceHold) {
$upn = $($_.userprincipalname)
Write-host "TEST $($_.userprincipalname)"
if ($each = Get-Mailbox -Identity $upn | where ($_.inplaceholds -eq $null)) {
foreach ($mailbox in $each) {
write-host "mailbox: $mailbox"
ForEach ($guid in $mailbox.$findParameter) {
$mailboxHash = @{}
$mailboxHash['InPlaceHoldName'] = ($hash[$guid]).name
$mailboxHash['StatusofHold'] = ($hash[$guid]).Status
$mailboxHash['StartDate'] = ($hash[$guid]).StartDate
$mailboxHash['EndDate'] = ($hash[$guid]).EndDate
$mailboxHash['ItemHoldPeriod'] = ($hash[$guid]).ItemHoldPeriod
foreach ($field in $mailboxProperties) {
$mailboxHash[$field] = ($mailbox.$field) -join ","
}
$resultArray += [psCustomObject]$mailboxHash
}
}
}
}
else {
$each = Get-Mailbox -Identity $_.userprincipalname
foreach ($mailbox in $each) {
ForEach ($guid in $mailbox.$findParameter) {
$mailboxHash = @{}
$mailboxHash['InPlaceHoldName'] = ($hash[$guid]).name
$mailboxHash['StatusofHold'] = ($hash[$guid]).Status
$mailboxHash['StartDate'] = ($hash[$guid]).StartDate
$mailboxHash['EndDate'] = ($hash[$guid]).EndDate
$mailboxHash['ItemHoldPeriod'] = ($hash[$guid]).ItemHoldPeriod
foreach ($field in $mailboxProperties) {
$mailboxHash[$field] = ($mailbox.$field) -join ","
}
$resultArray += [psCustomObject]$mailboxHash
}
}
}
}
End {
$resultArray | Select displayname, userprincipalname, InPlaceHoldName, IsInactiveMailbox, accountdisabled, ItemHoldPeriod, RecipientTypeDetails, StatusofHold, StartDate, EndDate, inplaceholds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment