Exchange Online Powershell get User without Address Book Policy
Hey,
I have the following problem. I have a script that assigns users to an ABP based on the attributes Company and Department.
function Set-ABPstoUser
{
Write-StatusLog -Type “SECTION” -Message “Assigne ABPs” -StatusFile $status_file
$abps = Get-AddressBookPolicy
$counter = 0
foreach ($abp in $abps)
{
$abpName = $abp.Name
$abpCompanyName = $abpName.Split(‘_’)[1]
$abpDepartment = $abpName.Split(‘_’)[2]
$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
Write-StatusLog -Type “SECTION” -Message “Log of Assigments” -StatusFile $status_file
foreach ($user in $usersWithCompanyAndDepartment)
{
Set-Mailbox -Identity $user.Identity -AddressBookPolicy $abpName
Write-Host “Die ABP ‘$abpName’ wurde der Mailbox ‘$($user.DisplayName)’ zugewiesen.”
$counter++
}
Write-StatusLog -Type “COMPLETE” -Message “Log of Assigments” -StatusFile $status_file
Write-StatusLog -Type “MESSAGE” -Message “$counter User geht assigned new ABP” -StatusFile $status_file
}
Write-StatusLog -Type “COMPLETE” -Message “Assigne ABPs” -StatusFile $status_file
}
There are currently 2 thousand mailboxes, but this number will rise to 50 thousand mailboxes. The script currently takes 8 minutes to assign 2 thousand mailboxes. The problem is that mailboxes that already have an ABP are also touched. This makes the script extremely long and unnecessary.
So far I have not been able to filter for mailboxes that do not have an ABP.
I have tried the following (the first one throws an error saying it cannot be filtered and the second one runs through all of them without any changes):
$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment’ -and AddressBookPolicy -eq `$null”$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
$usersWithoutABP = $usersWithCompanyAndDepartment | Where-Object { $_.AddressBookPolicy -eq $null }
$usersWithCompanyAndDepartment = Get-Mailbox -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
$usersWithoutABP = $usersWithCompanyAndDepartment | Where-Object { $_.AddressBookPolicy -eq $null }
Do you have any ideas what can be done?
Thank you very much!
Luca
Hey,I have the following problem. I have a script that assigns users to an ABP based on the attributes Company and Department. function Set-ABPstoUser
{
Write-StatusLog -Type “SECTION” -Message “Assigne ABPs” -StatusFile $status_file
$abps = Get-AddressBookPolicy
$counter = 0
foreach ($abp in $abps)
{
$abpName = $abp.Name
$abpCompanyName = $abpName.Split(‘_’)[1]
$abpDepartment = $abpName.Split(‘_’)[2]
$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
Write-StatusLog -Type “SECTION” -Message “Log of Assigments” -StatusFile $status_file
foreach ($user in $usersWithCompanyAndDepartment)
{
Set-Mailbox -Identity $user.Identity -AddressBookPolicy $abpName
Write-Host “Die ABP ‘$abpName’ wurde der Mailbox ‘$($user.DisplayName)’ zugewiesen.”
$counter++
}
Write-StatusLog -Type “COMPLETE” -Message “Log of Assigments” -StatusFile $status_file
Write-StatusLog -Type “MESSAGE” -Message “$counter User geht assigned new ABP” -StatusFile $status_file
}
Write-StatusLog -Type “COMPLETE” -Message “Assigne ABPs” -StatusFile $status_file
}There are currently 2 thousand mailboxes, but this number will rise to 50 thousand mailboxes. The script currently takes 8 minutes to assign 2 thousand mailboxes. The problem is that mailboxes that already have an ABP are also touched. This makes the script extremely long and unnecessary.So far I have not been able to filter for mailboxes that do not have an ABP.I have tried the following (the first one throws an error saying it cannot be filtered and the second one runs through all of them without any changes):$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment’ -and AddressBookPolicy -eq `$null”$usersWithCompanyAndDepartment = Get-User -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
$usersWithoutABP = $usersWithCompanyAndDepartment | Where-Object { $_.AddressBookPolicy -eq $null }
$usersWithCompanyAndDepartment = Get-Mailbox -Filter “Company -eq ‘$abpCompanyName’ -and Department -eq ‘$abpDepartment'”
$usersWithoutABP = $usersWithCompanyAndDepartment | Where-Object { $_.AddressBookPolicy -eq $null }Do you have any ideas what can be done?Thank you very much!Luca Read More