Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cmdlet parameter from variable 1

Status
Not open for further replies.
Aug 7, 2006
578
US
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.
 
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
 
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
 
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.
 
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.
 
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 '" +[b][COLOR=red]$dbase_name[/color][/b]+"'"
$org_param = "-OrganizationalUnit 'mydomain.com/"+[b][COLOR=red]$my_ou[/color][/b]+"/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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top