PS Script to have Argument for SamAccount Name in Exchange 2013 Hybrid-Office 365 enviroment

So we have a Hybrid environment with Office 365.  All mailboxes are in the cloud but we have mail routed through our Hybrid on premise server.  Were trying to use the ADManage tool as a 3rd part to create Domain user and have it create mailboxes at the same time.  We need to write a script that takes the below command that we normally run VIA the 2013 EMC on Exchange Hybrid server and have it have an argument for the SamAccount name to be placed in the beginning quotations.  If we can have the "Argument" successfully implemented into a power shell scritp then ADmanage support tells us when we sue their product to create a new user when we get to the 365 portion of their tool this script should run the powershell cmd and create a remote routing address as well as Office 365 account successfully.

""|foreach {Enable-RemoteMailbox -identity $_ -RemoteRoutingAddress "$_@agilysyscorp.mail"}
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

What's the source of the samaccountname?  A file that contains a bunch, a .CSV with lots of other info, or typed directly into a console one at a time?

For a plain text file, typically you would have something like
Get-Content yourfile.txt | foreach {Enable-RemoteMailbox -identity $_ -RemoteRoutingAddress "$"}

Open in new window

Twhite0909Author Commented:
Active Directory should be the source for the sam account.  I need this script to run for creating a new user is created in our domain.  This script will help the email on 365 get created automatically assuming the user account has already been created the SAMAccount should be pulled from AD.
SamAccountNames can be pulled from AD with a command like
Import-Module ActiveDirectory
Get-ADUser -filter * -searchbase "OU-someOU,DC=domain,DC=com" | Select -expandProperty samAccountName

Open in new window

You can pipe that into the ForEach command.  I have included the -searchbase parameter to limit the search to a specific OU and child OUs.  Of course it would have to be run on a system that has the ActiveDirectory module.

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
Office 365

From novice to tech pro — start learning today.