krish5music
asked on
Add condition for OU based mailbox creation.
Hello All,
I got the below script in Expert Exchange. This script checks for newly created AD account and enable the mailbox.
I wanted to include a condition like if the ad account belongs to separate OU, it should not enable the mailbox.
Can anyone please help me to achieve the above condition?
Import-Module ActiveDirectory
$lastday = ((Get-Date).AddDays(-1))
Get-ADUser -filter {(whencreated -ge $lastday)} -properties DisplayName,SamAccountName ,legacyExc hangeDN | Where{$_.legacyExchangeDN -eq $null} | Select DisplayName,SamAccountName ,legacyExc hangeDN | Sort DisplayName | Export-csv 'c:\temp\Acctswnomailbox.c sv' -notypeinformation
Import-csv 'c:\temp\Acctswnomailbox.c sv' | %{Enable-Mailbox $_.SamaccountName -database "DB02" -erroraction SilentlyContinue -verbose}
Thanks
Krish
I got the below script in Expert Exchange. This script checks for newly created AD account and enable the mailbox.
I wanted to include a condition like if the ad account belongs to separate OU, it should not enable the mailbox.
Can anyone please help me to achieve the above condition?
Import-Module ActiveDirectory
$lastday = ((Get-Date).AddDays(-1))
Get-ADUser -filter {(whencreated -ge $lastday)} -properties DisplayName,SamAccountName
Import-csv 'c:\temp\Acctswnomailbox.c
Thanks
Krish
Try this, I commented out the part that enables the mailboxes, so that you can check the csv if the results in the csv are what you expect them to be.
Import-Module ActiveDirectory
$csv = 'C:\temp\Acctswnomailbox.csv'
$lastday = ((Get-Date).AddDays(-1))
$excludeOUName = 'ExcludeMe'
$excludeDN = (Get-ADOrganizationalUnit -Filter "Name -eq '$($excludeOUName)'").DistinguishedName
Get-ADUser -Filter {(whenCreated -ge $lastday)} -Properties DisplayName, SamAccountName, legacyExchangeDN |
Where-Object {($_.legacyExchangeDN -eq $null) -and (($_.DistinguishedName -replace '\ACN=.*?,(?=OU=)') -ne $excludeDN)} |
Select-Object DisplayName, SamAccountName, legacyExchangeDN | Sort-Object -Property DisplayName |
Export-Csv -Path $csv -NoTypeInformation
Import-Csv -Path $csv | ForEach-Object {
# Enable-Mailbox $_.SamaccountName -Database "DB02" -ErrorAction SilentlyContinue -Verbose
}
Alright that's prettier than mine.....
ASKER
Hello Odba,
I have tested your script, however it is still creating mailbox for the AD account which is created under $excludeOUName.
Below is the script i have ran. Please let me know if i have do any changes.
Import-Module ActiveDirectory
$csv = 'C:\temp\Acctswnomailbox.c sv'
$lastday = ((Get-Date).AddDays(-1))
$excludeOUName = 'XFVD.com/Accounts/MSG/tes tuser'
$excludeDN = (Get-ADOrganizationalUnit -Filter "Name -eq '$($excludeOUName)'").Dist inguishedN ame
Get-ADUser -Filter {(whenCreated -ge $lastday)} -Properties DisplayName, SamAccountName, legacyExchangeDN |
Where-Object {($_.legacyExchangeDN -eq $null) -and (($_.DistinguishedName -replace '\ACN=.*?,(?=OU=)') -ne $excludeDN)} |
Select-Object DisplayName, SamAccountName, legacyExchangeDN | Sort-Object -Property DisplayName |
Export-Csv -Path $csv -NoTypeInformation
Import-Csv -Path $csv | ForEach-Object {
Enable-Mailbox $_.SamaccountName -Database "DB02" -ErrorAction SilentlyContinue -Verbose
}
Thanks
Krish
I have tested your script, however it is still creating mailbox for the AD account which is created under $excludeOUName.
Below is the script i have ran. Please let me know if i have do any changes.
Import-Module ActiveDirectory
$csv = 'C:\temp\Acctswnomailbox.c
$lastday = ((Get-Date).AddDays(-1))
$excludeOUName = 'XFVD.com/Accounts/MSG/tes
$excludeDN = (Get-ADOrganizationalUnit -Filter "Name -eq '$($excludeOUName)'").Dist
Get-ADUser -Filter {(whenCreated -ge $lastday)} -Properties DisplayName, SamAccountName, legacyExchangeDN |
Where-Object {($_.legacyExchangeDN -eq $null) -and (($_.DistinguishedName -replace '\ACN=.*?,(?=OU=)') -ne $excludeDN)} |
Select-Object DisplayName, SamAccountName, legacyExchangeDN | Sort-Object -Property DisplayName |
Export-Csv -Path $csv -NoTypeInformation
Import-Csv -Path $csv | ForEach-Object {
Enable-Mailbox $_.SamaccountName -Database "DB02" -ErrorAction SilentlyContinue -Verbose
}
Thanks
Krish
As the variable name (and the filter that retrieves the OU) indicates, $excludeOUName must be the Name of the OU, not the Canonical Name, nor the DistinguishedName.
This will now throw an error if the OU was not found.
This will now throw an error if the OU was not found.
Import-Module ActiveDirectory
$csv = 'C:\temp\Acctswnomailbox.csv'
$lastday = ((Get-Date).AddDays(-1))
$excludeOUName = 'testuser'
$excludeDN = (Get-ADOrganizationalUnit -Filter "Name -eq '$($excludeOUName)'").DistinguishedName
If (-not ($excludeDN)) {Throw "No OU with a name of '$($excludeOUName)' found!"}
Get-ADUser -Filter {(whenCreated -ge $lastday)} -Properties DisplayName, SamAccountName, legacyExchangeDN |
Where-Object {($_.legacyExchangeDN -eq $null) -and (($_.DistinguishedName -replace '\ACN=.*?,(?=OU=)') -ne $excludeDN)} |
Select-Object DisplayName, SamAccountName, legacyExchangeDN | Sort-Object -Property DisplayName |
Export-Csv -Path $csv -NoTypeInformation
Import-Csv -Path $csv | ForEach-Object {
# Enable-Mailbox $_.SamaccountName -Database "DB02" -ErrorAction SilentlyContinue -Verbose
}
ASKER
Hello Odba,
Thanks for your response. I just wanted to know , incase if i have multiple OU needs to be excluded then should i include like below?
$excludeOUName = 'testuser' , 'testuser2'
Please confirm.
Thanks
Krish
Thanks for your response. I just wanted to know , incase if i have multiple OU needs to be excluded then should i include like below?
$excludeOUName = 'testuser' , 'testuser2'
Please confirm.
Thanks
Krish
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a ton OBdA. There is no words to praise your expertise. I got the exact solution what i was expected.
Thanks again for taking time and responding patiently for my query.
Wish you a Happy New Year.
Regards
Krish.
Thanks again for taking time and responding patiently for my query.
Wish you a Happy New Year.
Regards
Krish.
So basically, if you have all your users in a specific OU I.E the ones you want to have mailboxes, put the distingished name in "DN of OU"
Regards
Alex