SOLVED

Find user without mailbox

%3CLINGO-SUB%20id%3D%22lingo-sub-146256%22%20slang%3D%22en-US%22%3EFind%20user%20without%20mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-146256%22%20slang%3D%22en-US%22%3E%3CP%3EHi.%20Im%20trying%20to%20create%20a%20script%20that%20loads%20users%20from%20an%20AD%20Group%20and%20filter%20them%20based%20on%20an%20attribute.%20My%20goal%20is%20to%20have%20a%20script%20that%20loads%20users%20from%20a%20group%2C%20finde%20the%20ones%20that%20do%20not%20have%20a%20mailbox%20in%20my%20exchange%20inviroment%2C%20and%20mail%20enable%20them.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERight%20now%20im%20stuck%20on%20the%20%22finde%20the%20users%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMy%20Scirpt%20so%20far%3A%3C%2FP%3E%0A%3CP%3E%23----------------------------------------------------------%20%3CBR%20%2F%3E%23%20LOAD%20AD%20MODULES%20%3CBR%20%2F%3E%23----------------------------------------------------------%20%3CBR%20%2F%3EImport-Module%20ActiveDirectory%20-ErrorAction%20Stop%3C%2FP%3E%0A%3CP%3E%23----------------------------------------------------------%20%3CBR%20%2F%3E%23%20IMPORT%26nbsp%3B%20MEMBERS%20OF%20GROUP%3CBR%20%2F%3E%23----------------------------------------------------------%20%3CBR%20%2F%3E%24Users%20%3D%20Get-ADGroupMember%20-Identity%20%22S_Enable_Exch2010User%22%3CBR%20%2F%3E%24Users%20%7C%20ForEach%7BGet-ADUser%20-filter%20%7B-not(msExchMailboxGuid%20-like%20%22*%22)%7D%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHow%20ever%2C%20this%20looks%20at%20all%20users%20in%20my%20AD%20and%20not%20the%20ones%20in%20%24Users.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20am%20I%20doing%20wrong....%3F%3F%3F%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-146256%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-150942%22%20slang%3D%22en-US%22%3ERe%3A%20Find%20user%20without%20mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-150942%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYes%2C%26nbsp%3Bif%20you%20don't%20pipe%20%24_%20in%20ForEach%20statement%20then%20you%20will%20get%20All%20Users.%20Try%20this%20instead%20(replace%20'mydomain.com'%20with%20your%20desired%20domain%2C%20and%20samaccountname%20with%20the%20property%20you%20want)%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%24Users%20%3D%20Get-ADGroupMember%20-Identity%20%22S_Enable_Exch2010User%22%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EForEach%20(%24User%20in%20%24Users)%20%7B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20!(%24_.msExchMailboxGuid)%20%7B%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20Set-ADUser%26nbsp%3B-Identity%20%24User%20-EmailAddress%20(%24_.samaccountname%20%2B%20'%40mydomain.com')%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-146558%22%20slang%3D%22en-US%22%3ERe%3A%20Find%20user%20without%20mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-146558%22%20slang%3D%22en-US%22%3E%3CP%3EWhy%20don't%20you%20simply%20use%20the%20Exchange%20cmdlets%20for%20that%3F%20Get-Mailbox%20or%20Get-Recipient%20will%20do%20the%20tasks%20with%20ease.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20to%20answer%20the%20specific%20question%20about%20the%20cmdlet%20-%20you%20are%20not%20referencing%20the%20%24users%20value%20in%20any%20way%20or%20form%2C%20thus%20the%20cmdlet%20runs%20against%20the%20whole%20of%20AD.%20Add%20it%20to%20the%20filter.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-146545%22%20slang%3D%22en-US%22%3ERe%3A%20Find%20user%20without%20mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-146545%22%20slang%3D%22en-US%22%3E%3CP%3EI%20dont%20have%20access%20to%20AD%20right%20now%2C%20but%20i%20think%20you%20could%20do%20something%20like%20this.%20!%20in%20front%20of%20%24user.msExchMailboxGuid%20means%20that%20if%20the%20variable%20is%20null(empty)%2C%20it%20should%20create%20a%20mailbox%2C%20if%20its%20already%20have%20a%20mailbox%2C%20a%20message%20stating%20this%20is%20dispalyed.%20Hope%20this%20works%2C%20or%20sett%20you%20on%20the%20right%20track.%3C%2FP%3E%0A%3CPRE%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%24Users%20%3D%20Get-ADGroupMember%20-Identity%20%22S_Enable_Exch2010User%22%20%7C%20Get-ADUser%20-filter%20%7BmsExchMailboxGuid%20-like%20%22*%22%7D%0A%0Aforeach%20(%24user%20in%20%24users)%20%7B%0A%0Aif(!%24user.msExchMailboxGuid)%20%7B%0A%20%20%20%20enable%20mailbox%20command%0A%7D%0Aelse%20%7B%0A%20%20%20%20%24user.firstname%2B%22already%20have%20a%20email%20account.%22%0A%7D%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
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')

    }