如果未提供值,如何从cmdlet省略powershell参数? [英] How to omit powershell parameter from cmdlet if value not provided?
问题描述
我正在尝试执行以下Powershell命令,以使用名称,容器等参数在交换服务器上创建新的地址列表.
I'm trying to execute a below Powershell command to create a new address list on exchange server with parameters like Name, Container, etc.
Container
是可选的输入/参数,如果未提供cmdlet的值,如何从cmdlet中忽略它?
Container
is an optional input/parameter, how do I omit it from cmdlet if its value is not provided?
我尝试使用IF
条件句,但似乎不起作用.这里有帮助吗?
I tried with IF
conditionals but but does not seems working. Any help here?
New-AddressList -Name -Container \test MyAddressList5 -ConditionalStateOrProvince maha -IncludedRecipients MailboxUsers
推荐答案
您可以通过 hashtable 传递所需的参数及其相应的值.添加If/Else
条件以包括属性.像这样:
You can pass needed parameters with their corresponding values via hashtable. Add If/Else
conditions to include properties. Like so:
$Container = '\test MyAddressList5'
$Parameters = @{}
$Parameters.Add('ConditionalStateOrProvince','maha')
$Parameters.Add('IncludedRecipients','MailboxUsers')
if($Container){$Parameters.Add('Container',$Container)}
New-AddressList @Parameters
此外,当您需要包含Switch
参数时,只需传递$True
.像这样:
Also, when you need to include Switch
parameter just pass $True
. Like so:
$Parameters.Add('SomeSwitchParameter',$True)
这篇关于如果未提供值,如何从cmdlet省略powershell参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!