Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

blister911 (MIS) (OP)
15 Dec 11 11:03
I'm looking to pass a parameter into the New-Mailbox cmdlet, but it won't accept it.

Basically what I'm doing is trying to do is create a resource or shared mailbox based on user input.  I get all the information but I can't seem to get the command to accept the type of mailbox (-room, -equipment, -shared).  If I hard-code the parameters it works, but then I have to have 3 blocks of almost identical code. (We change some other mailbox settings, too)

(Not actual code)
$mbox = "Conference_Room"
$mbox_upn = "Conference_Room@mydomain.com"
$grp_ou = "mydomain.com/Resources"

#Display
1. Room
2. Equipment
3. Shared Mailbox

$resource_type = read-host "Enter type of mailbox to create."
switch ($resource_type)
{
0{$null}
1{$resource = "-room"}
2{$resource = "-equipment"}
3{$resource = "-shared"}
}

New-Mailbox -Name $mbox -Alias $mbox -OrganizationalUnit $grp_ou -UserPrincipalName $mbox_upn -SamAccountName $mbox-Database $dbase_name $resource

This seems like it should be easy to do, but I can't seem to make it work.

Thanks.

   
Light travels faster than sound. That's why some people appear bright until you hear them speak.

crobin1 (MIS)
15 Dec 11 17:02
I think what you want to use is what is known as splatting. I haven't tried it, but start with this "Hey, Scripting Guy!" article (and there's a link to other articles about splatting)
http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/29/use-powershell-splatting-to-simplify-parameter-sets.aspx
mrmovie (TechnicalUser)
16 Dec 11 9:02
is it not something to do with " or ', the way it is interepting the qoutes, try messing with single qoutes somewhere?

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald

Helpful Member!  mrmovie (TechnicalUser)
16 Dec 11 9:03
or perhaps try using Invoke-Expression? this might interept the string of your command better?

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald

blister911 (MIS) (OP)
16 Dec 11 10:42
crobin1 - I've played with the splatting a little, but couldn't get it to work.  I'll give it another try and see if I have different results.

mrmovie - I've done a lot with the quotes and apostrophes.  Nothing works.  I haven't looked at Invoke-Expression, yet.  I'll check into that.  Thanks for the suggestion.

BTW here is the error I get from the code above:
New-Mailbox : A positional parameter cannot be found that accepts argument '-room'

   
Light travels faster than sound. That's why some people appear bright until you hear them speak.

blister911 (MIS) (OP)
16 Dec 11 11:05
crobin1,
Maybe I'm missing something, but I still get an error with the splatting, because I have a parameter to assign the $resource switch to.

i.e. $params = @{'Name'=$mbox; 'Alias'=$mbox; ???=$resource}

New-Mailbox @params

   
Light travels faster than sound. That's why some people appear bright until you hear them speak.

blister911 (MIS) (OP)
16 Dec 11 14:27
Well, I got it to work.  I took mrmovie's suggestion to persure the invoke-expression command.  I had to set a couple of other variables for switches that could contain spaces (coded in red, below).  
Possible examples are:  $dbase_name = "Mailbox Database" and $my_ou = "My Organization"
I may go back and do it with all the parameters just to be safe, but for now I have it working and I'm moving forward with the script.

Here is a simplified version of the commands I used:

CODE

#Display
write-host "1. Room"
write-host "2. Equipment"
write-host "3. Shared Mailbox"

$resource_type = read-host "Enter type of mailbox to create."
switch ($resource_type)
{
0{$null}
1{$resource = "-room"}
2{$resource = "-equipment"}
3{$resource = "-shared"}
}
$dbase_param = "-database '" +$dbase_name+"'"
$org_param = "-OrganizationalUnit 'mydomain.com/"+$my_ou+"/Accounts/Resources'"

$create_resource_mailbox = "New-Mailbox -Name $mbox -Alias $mbox $org_param -UserPrincipalName $mbox_upn -SamAccountName $sam_name -FirstName '' -Initials '' -LastName '' $dbase_param -domaincontroller $domain_controller $resource"
invoke-expression $create_resource_mailbox | out-null

Thanks for the help.

   
Light travels faster than sound. That's why some people appear bright until you hear them speak.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close