Reputation: 193
I'm attempting to create new resources in Exchange Online via a script and it works if I type the line manually but when I run the script, the command New-Mailbox suddenly can't accept the "-Equipment" argument.
Script fails on the following row:
New-Mailbox -Name "$($Resource)" -$($Type)
Error shows following:
A positional parameter cannot be found that accepts argument '-Equipment'.
+ CategoryInfo : InvalidArgument: (:) [New-Mailbox], ParameterBindingException"
Upvotes: 0
Views: 441
Reputation: 174545
PowerShell interprets -$($Type)
as a string argument rather than a parameter name. Use splatting to conditionally pass parameters like this:
$extraParams = @{ $Type = $true }
New-Mailbox -Name "$($Resource)" @extraParams
I'm not sure which other types of mailboxes are available in Exchange Online, but you'll probably want to figure that out and apply some input validation:
param(
[string]$Resource,
[ValidateSet('Equipment','Person','Room')]
[string]$Type
)
# do other stuff here
# If someone passed a wrong kind of `$Type`, the script would have already thrown an error
$extraParams = @{ $Type = $true }
New-Mailbox -Name "$($Resource)" @extraParams
Upvotes: 2