1
votes

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

2
According to the document, Set-CASMailbox has an enormous amount of parameters. Make sure you're naming them. - Maximilian Burszley

2 Answers

3
votes

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
0
votes
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.