need to set CustomAttribute based on mailboxGUID

Experts ... .rather basic PowerShell skills - I need a way to get GUID value from a set of mailboxes, and put that value into CustomAttribute1 for exchange. I've got this:

The problem is that the GUID inserted into CustomAttribute1 is the GUID from the first mailbox, so all get the same error.
Something here... ??? (set-mailbox $user -CustomAttribute1 "iXOS-EXLK:/{$($GUID.Guid)}" ???)
 ####################################################################################################
 #
 # Script for retrieving members of  group, extract  GUID and set
 # CustomAttribute1 generated from iXOS-EXLK:/+GUID
 #
 #
 ######################################################################################################

 # Get users from AD-Group
 # Alternatively - Group can be entered by user
 #
 # $AdGroup = Read-Host "Enter group name"
 # $users = Get-ADGroupMember $AdGroup | select -ExpandProperty name
 #
 # Selecting Users from Group Test_MBX_Permission

 $users = Get-ADGroupMember Test_MBX_Permission | Select-Object -ExpandProperty name
 foreach ($user in $users)
{
 # Extract Mailbox GUID and pipe to variable $GUID
$GUID = Get-Mailbox $user | select GUID
}
# Set CustomAttribute1 based on iXOX-EXLK:/ + Users unique MailboxGUID
 foreach ($user in $Users)
 {
 $customAttrib1 = Get-Mailbox -Identity $user -CustomAttribute1 -ErrorAction SilentlyContinue | select CustomAttribute1
 if($customAttrib1)
 {
 #If customattribute1 have value - do nothing
 }
 else
 {
#set-mailbox with value CustomATtribute1
 set-mailbox $user -CustomAttribute1 "iXOS-EXLK:/{$($GUID.Guid)}"
 }

 }
LVL 22
Jakob DigranesSenior ConsultantAsked:
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.

footechCommented:
You're doing the same loop twice.  When you finish the first foreach loop, $GUID will have the last value that it was set to, it won't change after that.  It's also best to combine the two Get-Mailbox commands so that the query is made only once to retrieve all needed info.
$users = Get-ADGroupMember Test_MBX_Permission | Select-Object -ExpandProperty name
foreach ($user in $users) 
{
    $userinfo = Get-Mailbox -Identity $user -ErrorAction SilentlyContinue | select CustomAttribute1,GUID
    if ($userinfo.CustomAttribute1)
    {
        #If customattribute1 have value - do nothing
    }
    else
    {
        # Set CustomAttribute1 based on iXOX-EXLK:/ + Users unique MailboxGUID
        #set-mailbox with value CustomAttribute1
        Set-Mailbox $user -CustomAttribute1 "iXOS-EXLK:/{$($userinfo.Guid)}"
    } 
}

Open in new window

0

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
Jakob DigranesSenior ConsultantAuthor Commented:
Absolutely spot-on !!! Thanks - the Powershell n00b -> getting smarter every day. Love it when you add explanation aswell :-)
0
footechCommented:
Glad I could help.  Thanks for the feedback.  :)
0
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
Exchange

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.