SOLVED

Find user without mailbox

Contributor

Hi. Im trying to create a script that loads users from an AD Group and filter them based on an attribute. My goal is to have a script that loads users from a group, finde the ones that do not have a mailbox in my exchange inviroment, and mail enable them.

 

Right now im stuck on the "finde the users".

 

My Scirpt so far:

#----------------------------------------------------------
# LOAD AD MODULES
#----------------------------------------------------------
Import-Module ActiveDirectory -ErrorAction Stop

#----------------------------------------------------------
# IMPORT  MEMBERS OF GROUP
#----------------------------------------------------------
$Users = Get-ADGroupMember -Identity "S_Enable_Exch2010User"
$Users | ForEach{Get-ADUser -filter {-not(msExchMailboxGuid -like "*")}}

 

How ever, this looks at all users in my AD and not the ones in $Users.

 

What am I doing wrong....????

3 Replies
best response confirmed by Jesper Stein (Contributor)
Solution

I dont have access to AD right now, but i think you could do something like this. ! in front of $user.msExchMailboxGuid means that if the variable is null(empty), it should create a mailbox, if its already have a mailbox, a message stating this is dispalyed. Hope this works, or sett you on the right track.



$Users = Get-ADGroupMember -Identity "S_Enable_Exch2010User" | Get-ADUser -filter {msExchMailboxGuid -like "*"} foreach ($user in $users) { if(!$user.msExchMailboxGuid) { enable mailbox command } else { $user.firstname+"already have a email account." } }

Why don't you simply use the Exchange cmdlets for that? Get-Mailbox or Get-Recipient will do the tasks with ease.

 

And to answer the specific question about the cmdlet - you are not referencing the $users value in any way or form, thus the cmdlet runs against the whole of AD. Add it to the filter.

 

Yes, if you don't pipe $_ in ForEach statement then you will get All Users. Try this instead (replace 'mydomain.com' with your desired domain, and samaccountname with the property you want):

 

$Users = Get-ADGroupMember -Identity "S_Enable_Exch2010User"

ForEach ($User in $Users) {

If !($_.msExchMailboxGuid) { 

    Set-ADUser -Identity $User -EmailAddress ($_.samaccountname + '@mydomain.com')

    }