gpt4 book ai didi

powershell - 在变量Powershell中传递参数

转载 作者:行者123 更新时间:2023-12-03 01:05:10 26 4
gpt4 key购买 nike

我正在尝试禁用场外脚本的Email Apps上的“Azure admin center”功能。我不确定是否可以将一些参数传递到foreach循环中,以下是代码。我已经运行了定义了参数和没有定义参数的代码。

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

我遇到的错误是-

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

最佳答案

我认为您可能正在寻找的叫做splatting

如果在哈希表中定义参数,则在将@用作splat运算符时,可以将它们作为参数传递。 (请注意,@除了是splat运算符之外,还有其他用途。)

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
}

然后,通过使用脚本参数,可以使用 -DisableApps将这些参数设置为false。
.\examplescript.ps1 -Identity 'differentuser' -DisableApps

关于powershell - 在变量Powershell中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com