Add condition for OU based mailbox creation.

krish5music
krish5music used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

AlexSenior Infrastructure Analyst

Commented:
Alright that's prettier than mine.....
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
No, the current script only supports one OU.
This now accepts a list:
Import-Module ActiveDirectory

$csv = 'C:\temp\Acctswnomailbox.csv'
$lastday = ((Get-Date).AddDays(-1))
$excludeOUNames = 'testuser' , 'testuser2'

$excludeDNs = $excludeOUNames | ForEach-Object {
	If ($dn = (Get-ADOrganizationalUnit -Filter "Name -eq '$($_)'").DistinguishedName) {
		$dn
	} Else {
		Throw "No OU with a name of '$($_)' found!"
	}
}
Get-ADUser -Filter {(whenCreated -ge $lastday)} -Properties DisplayName, SamAccountName, legacyExchangeDN |
	Where-Object {($_.legacyExchangeDN -eq $null) -and ($excludeDNs -notcontains ($_.DistinguishedName -replace '\ACN=.*?,(?=OU=)')} |
	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

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial