I am working on a script to modify all new mailboxes created within the last X days. Plan to use this to apply mailbox retention policies to these new mailboxes each evening with a scheduled script. After watching a short web seminar on powershell.com, I thought I'd create it as a function that has some required parameters.
Reference: http://www.wisesoft.co.uk/scripts/powershell_search_for_all_users_by_the...
function Get-NewMailboxes {
<#
.Synopsis Return all mailboxes created after a specific number of days back
#>
param (
[parameter(Mandatory = $true, ValueFromPipeline=$true)]
[int]$DaysBack
)
if ($daysback -gt 0) {$daysBack = $daysback * -1}
#All Mailboxes created in the last 24 hours.
$searchDate = (Get-Date).AddDays($DaysBack) | Get-Date -UFormat "%Y%m%d000000.0Z"
$strFilter = "(&(objectCategory=User)(objectclass=user)(whencreated>=$searchDate))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#$colProplist = "distinguishedname"
#foreach ($i in $colPropList){
$objSearcher.PropertiesToLoad.Add("distinguishedname") | out-Null
#}
$colResults = $objSearcher.FindAll()
$mbxes = @()
foreach ($objResult in $colResults) {
$objItem = $objResult.Properties;
$mbxes += Get-mailbox -resultsize unlimited -id $objitem.distinguishedname[0].ToString()
}
return $mbxes
}
Use:
get-newMailboxes -daysBack 1 | set-casmailbox -activesyncEnabled $false
Comments
Post new comment