Link to home
Start Free TrialLog in
Avatar of krish5music
krish5musicFlag for United States of America

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,legacyExchangeDN | Where{$_.legacyExchangeDN -eq $null} | Select DisplayName,SamAccountName,legacyExchangeDN | Sort DisplayName | Export-csv 'c:\temp\Acctswnomailbox.csv' -notypeinformation
Import-csv 'c:\temp\Acctswnomailbox.csv' | %{Enable-Mailbox $_.SamaccountName -database "DB02" -erroraction SilentlyContinue -verbose}

Thanks
Krish
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

Get-ADUser -filter {(whencreated -ge $lastday)} -properties DisplayName,SamAccountName,legacyExchangeDN -searchbase "DN of OU"

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
Avatar of oBdA
oBdA

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
}

Open in new window

Alright that's prettier than mine.....
Avatar of krish5music

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.csv'
$lastday = ((Get-Date).AddDays(-1))
$excludeOUName = 'XFVD.com/Accounts/MSG/testuser'

$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
}

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.
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
}

Open in new window

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.