How to omit powershell parameter from cmdlet if value not provided?

前端 未结 3 1804
青春惊慌失措
青春惊慌失措 2021-01-27 07:21

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 is an

3条回答
  •  孤街浪徒
    2021-01-27 07:53

    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
    

    Also, when you need to include Switch parameter just pass $True. Like so:

    $Parameters.Add('SomeSwitchParameter',$True)
    

提交回复
热议问题