How to set an automatic X500 SMTP address based on the usermailbox

Hi Experts,

I apologise with my basic request in powershell.
I need to automate some tasks at work and I am trying to put things togheter to have a kind of script in the end.
I need to get all "usermailbox" from a specific organization unit and set automagically a X500 SMTP address.

So far this is why I could reach >>>


Get-User –OrganizationalUnit "OU=Provisional,OU=ORGUNIT,DC=domain,DC=com" | Where-Object{$_.RecipientType –eq “UserMailbox”} | Set-Mailbox $SAMAccountname -EmailAddresses @{Add=’X500:/o=DOMAIN/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=PrimarySmtpAddress'}

Notes:
$SAMAccountname is the accountname from the user
PrimarySmtpAddress is the primary smtp address from the UserMailbox.


The error>>
Cannot bind argument to parameter 'Identity' because it is null.
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Set-Mailbox


I guess my problem now is the $SAMAccountname.
Can someone put in the right direction?
LVL 1
osagaranaAsked:
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.

SubsunCommented:
Try..
Get-Mailbox –OrganizationalUnit "OU=Provisional,OU=ORGUNIT,DC=domain,DC=com" | Where-Object{$_.RecipientType -eq "UserMailbox"} | % {Set-Mailbox $_.SAMAccountname -EmailAddresses @{Add="X500:/o=DOMAIN/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=$($_.PrimarySmtpAddress.tostring())"}}

Open in new window

Or
Foreach ($User in (Get-Mailbox –OrganizationalUnit "OU=Provisional,OU=ORGUNIT,DC=domain,DC=com" | Where-Object{$_.RecipientType -eq "UserMailbox"})) {
Set-Mailbox $User.SAMAccountname -EmailAddresses @{Add="X500:/o=DOMAIN/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=$($User.PrimarySmtpAddress.tostring())"}
}

Open in new window

0
osagaranaAuthor Commented:
Hi Subsun,

I guess I am almost there.
I had two mailboxes there and SMTP was added just for one object.

Here is the error:


[PS] C:\scripts>Get-mailbox -OrganizationalUnit "OU=Users,OU=MOE,DC=domain,DC=com" | Where-Object{$_.RecipientType -eq "UserMailbox"} | % {Set-Mailbox $_.SAMAccountname -EmailAddresses @{Add="X500:/o=domain/ou=Exchange Administrative Group FYDIBOHF23SPDLT)/cn=Recipients/cn=$($_.PrimarySmtpAddress.tostring())"}}
Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
   PSInvalidOperationException
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
   PSInvalidOperationException
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

[PS] C:\scripts>
0
SubsunCommented:
Did you try the second code? It should work without the pipeline error..
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
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.