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
LVL 7
krish5musicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex GreenProject Systems EngineerCommented:
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
oBdACommented:
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

Alex GreenProject Systems EngineerCommented:
Alright that's prettier than mine.....
Determine the Perfect Price for Your IT Services

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

krish5musicAuthor 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
oBdACommented:
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

krish5musicAuthor 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
oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krish5musicAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.