I have a basic script for creating shared mailboxes in exchange that I modify each time to reflect if the mailbox is room, shared or equipment. I have not found a way for the script to accept that information in a variable. Script below. The -room (-shared, -equipment) is what I am trying to use a variable for.
I am not quite sure how that applies to the -room, -shared -equipment qualifier though. I don’t have a problem with any of the other variables, they work well. It’s only that one qualifier to the new-mailbox xxxxx -room.
So I am not sure how I would incorporate that into the splatting technique.
The only problem with doing it that way is that you have to duplicate the actual commands new-mailbox etc for each switch value. I am trying to avoid that.
When I create rooms and use Powershell, it’s because I am creating multiples and I use a csv file for the information. So the hope was that a value in the csv (room, shared, equipment) could be used instead of -room, it would be -$_.type with type being the value room, shared, equipment.
This way the script is a foreach that recognizes for each row whether its a room mailbox, shared mailbox or equipment mailbox.
Hmmm … I’m a little confused now. You just would have to make a function of it and pass whatever you need as a parameter to it. Or that even would work with script parameters. Just make them available ‘from outside’. Or do I get something wrong?
I think he may be confused by how the switch parameters work. These 2 statements should be the same.
New-mailbox -Room
New-mailbox -Room:$true
So Olaf is only adding the one that’s relevant, and he’s setting the value to $true. I haven’t used splatting a lot, so I don’t know if setting Room to $null would accomplish the same thing as the first statement above or not.
Hmmm … but -Room is not set to $Null in my example. Either it is $true or not existing, what means $false - just like a any other switch parameter you use. You might just give it a try. It won’t hurt you.
-Room
The Room parameter specifies that the type of resource is a room, if this mailbox is a resource mailbox. This parameter is required only if you're creating a resource mailbox.
Required? true
Position? Named
Default value
Accept pipeline input? False
Accept wildcard characters? false
It’s required to create a room mailbox but has no default value. I haven’t tested it, but that implies to me that the value is irrelevant, it just needs to be specified.
Your first code example produces the same result as mine.
In my opinion in your second example will the ‘$false’ not be the value for the switch parameter it will be taken as another/next parameter. If you like to explicitly assign ‘$false’ to a switch parameter you have to use the way you alraedy showed -switch:$false
Exactly. It is the value in and of itself. I think that both of you are misunderstanding my request though. Let me try this again. First there are 3 options when creating a resource mailbox, -room, -shared or -equipment. These options give you specific things, like an auto booking attendant in the room and equipment mailboxes. It is necessary to specify what TYPE of resource mailbox it is and these are how you do that.
If I am creating a single resource mailbox, I do it in the console. If I am creating multiple mailboxes, I specify all the params in a csv file, import it and use a foreach to create the mailboxes. I want to be able to specify the resource type -room (-equipment, -shared) in the csv file.
So my csv file would look something like this:
Mailboxname alias database type
Sales Calendar scalendar xxxdb1 room
Scale Tester stester xxxxdb2 equipment
Atlanta Candy acandy atlxxxdb2 shared
This way all I need is a foreach loop that creates each mailbox using the values from the csv. I don’t want to have to use any if, if else or switch functions to do this as it would defeat the entire purpose of not having to duplicate the command line 3 times.
If I was only creating room mailboxes it would look something like this:
So to create multiple resource mailboxes with different types, it would look something like this, where -$i.type is the resource type room, shared or equipment. I have tried it this way using the variable value and it doesn’t see it properly.
Feel free to ignore this, I’m just working out my own misunderstandings.
My inexperience with advance parameter processing is showing. I assumed an intuitive function for IsPreset, when its actually the same as the value, and only used for switch parameters. Here is what I thought I was demonstrating…
The last 3 splatted switch tests are the ones relevant to the original problem. Parameter existence and value can be determined separately, and if no value is specified and no default is defined, it evaluates to false.
Add an additional key if you want to create it as a room.
If($RoomParam){
$Parms.Room = $true
}
If you don’t add that additional key, it’s the same as calling new-mailbox without -room. So you need to determine which single additional key to add, Room, Equipment or Shared.
Will produce an error because it will try to interpret the “$param” as paramenter value, not a parameter.
One workaround would be to create the command line and then use Invoke-Expression, but Invoke-Expression is not really considered best practice to use
Ok, I was wrong, I still don’t understand what is going on here. I am good up to this point:
If($Room){
$Parms.Room = $true
So you have defined $Room, $Shared and $Equipment as switches which I presume is what will add the -room, -equipment etc to the command line. But how is it determining if $room is true? Where is it getting the value from to say that this particular set of parameters is for a room, shared or equipment mailbox?