1

I am trying to disable "Email Apps" features on Azure admin center for an off-boarding script. I'm not sure if I can pass a few parameters into a foreach loop, below is the code. I've ran the code with param defined and without param defined.

param(
  [parameter]$DisableApps
)
$DisableApps = ("OWAEnabled" , "EwsEnabled" , "PopEnabled")
foreach ($i in $DisableApps){Set-CASMailbox "Sahand.Test" $i $false}

The error I am getting is -

A positional parameter cannot be found that accepts argument '-OWAEnabled'. + CategoryInfo : InvalidArgument: (:) [Set-CASMailbox], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Set-CASMailbox + PSComputerName : outlook.office365.com

1

2 Answers 2

3

What I think you may be looking for is called splatting

If you define your parameters in a hashtable, you can pass them as parameters when you use @ as a splat operator. (Note that @ has other uses besides being the splat operator.)

param(
    [string]$Identity = "Sahand.Test",
    [switch]$DisableApps 
)
If ($DisableApps) {
    $CASMailboxParams = @{
        Identity =  $Identity
        OWAEnabled = $False
        EwsEnabled = $False
        PopEnabled = $False
    }
    Set-CASMailbox @CASMailboxParams
} else {
    Set-CASMailbox $Identity
}

Then by using your script parameters, you can use -DisableApps to set the those parameters to false.

.\examplescript.ps1 -Identity 'differentuser' -DisableApps
Sign up to request clarification or add additional context in comments.

3 Comments

Not sure @ is a "splat operator" considering its use in the array and hashtable syntaxes.
@TheIncorrigible1 From the linked TechNet article on Splatting: The “@” sign, when used as a splat operator, does something similar. Answer updated for clarity
Thank you for your help, I ended up modifying your code. I have read splatting once before when I was trying to invoke commands and had to define parameters.
0
param([bool]$CASMailboxParams, [string]$User)
$User = "Sahand.Test"
$CASMailboxParams = @{
    OWAEnabled = $False
    EwsEnabled = $False
    PopEnabled = $False
}
        Set-CASMailbox -Identity $User @CASMailboxParams
        Get-CASMailbox $User                

This will allow you to pass parameters through a command and check it the casmailbox at the end. Note:This script is for Exchange Powershell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.