Powershell pass parameter help

Powershell Script to pass parameter, accept input.

I'm trying to build a script that I can pass a parameter to make a remote mailbox.

Using the below take "testuser1" to pass the parameter outside of this script.

Enable-RemoteMailbox "testuser1@microsoft.com" -RemoteRoutingAddress testuser1@company.mail.onmicrosoft.com

Then after this, have something that will validate the smtp address.

Can any help with this?

Thanks!
Rob ShortsAsked:
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.

Vasil Michev (MVP)Commented:
Simply put the parameter block on top of the script, just like you would do for functions, this article should get you started: https://technet.microsoft.com/en-us/library/jj554301.aspx

In your case, you can put a "mailbox" or "identity" string-variable as parameter. For validation, you can go as simple as checking the presence of "@" sign, or as complex as using one of the built-in SMTP objects. For example,

[Net.Mail.MailAddress]$mailbox = "user@domain.com"

Open in new window

For more advanced scenarios of passing parameters, check the splatting concept: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6
0
Rob ShortsAuthor Commented:
Thanks Vasil, much appreciated.  What I have below is working in creating the mb, are you saying in the same code check the "@" sign or use what you listed above?  Can you list an example please?

Also, when provisioning a mailbox, I have a hybrid env. so I need to license after the remote mailbox is created, which takes up to 30 minutes sometimes by adding to a group... is there anyway to include this in one code, or how do you get around this in a hybrid environment, any suggestions?

param
(
  $mailbox
)
Enable-RemoteMailbox $mailbox@microsoft.com -RemoteRoutingAddress "$mailbox@company.mail.onmicrosoft.com"

Open in new window

0
Vasil Michev (MVP)Commented:
Actually, come to think of it I believe it's better to skip the check. Reason being, the Enable-RemoteMailbox can work against multiple different values for the identity parameter, not necessarily representing an email address. The GUID will be a valid value, so will be the alias, etc. If you still want to do a check for valid mail addresses, there's a sample regex here: https://stackoverflow.com/questions/48253743/powershell-to-validate-email-addresses

If you are already using a group to license the users in O365, it's best to keep using that method so that all your users licenses are managed in the same fashion. Plus, completing the remote mailbox creation process will depend on running a dirsync cycle as well, so you have to wait anyway. If you want to do the licensing as part of the code, you will need to use the MSONline or the AzureAD module. There's a very detailed explanation of the process here: https://docs.microsoft.com/en-us/office365/enterprise/powershell/assign-licenses-to-user-accounts-with-office-365-powershell
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
Powershell

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.