CyBeRkId2002 Posted November 11, 2021 Posted November 11, 2021 I have a script I would like to run hourly to place users in the correct OU. It works now, but is fairly slow as it goes over every user and tries to assign them to the ABP, even if it is already set. I am unsure how to improve the code so it checks if AddressBookPolicy is already set... I have used Get-User as I need to lookup Company / Department, and this does not allowpull through AddressBookPolicy as a filter item. Get-User -ResultSize unlimited -Filter "company -eq 'Trust'" | foreach {Set-Mailbox -Identity $_.MicrosoftOnlineServicesID -AddressBookPolicy "Trust"} Get-User -ResultSize unlimited -Filter {(UserPrincipalName -like "*@schooldomain.co.uk") -and (Department -ne "Governor") -and (RecipientType -eq "UserMailBox")} | foreach {Set-Mailbox -Identity $_.MicrosoftOnlineServicesID -AddressBookPolicy "SchoolABP"} Get-User -ResultSize unlimited -Filter {(UserPrincipalName -like "*@schooldomain.co.uk") -and (Department -eq "Governor") -and (RecipientType -eq "UserMailBox")} | foreach {Set-Mailbox -Identity $_.MicrosoftOnlineServicesID -AddressBookPolicy "GovernorABP"}
Steve21 Posted November 11, 2021 Posted November 11, 2021 Haven't tested it as we don't have those identities like company filled in, but couldn't you just use recipient as they have access to both company and ABP Something like: Get-Recipient -Filter "(RecipientType -eq 'UserMailbox')" | Where-Object {($_.Company -eq "Trust") -and ($_.AddressBookPolicy -eq $null)} Or: Get-Recipient -Filter "(RecipientType -eq 'UserMailbox') -and (Company -eq 'Trust') -and (AddressBookPolicy -notlike '*')" Steve
HPlum78 Posted November 12, 2021 Posted November 12, 2021 @Steve21 makes a good point and I am going to add this try to use the v2 commands as they have been reworked and are more performant that the original cmdlets. So Get-EXORecipient for instance, here is the reference for info:- https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now