简体   繁体   中英

Powershell - check if cmdlet has parameter

Is it possible to determine if certain cmdlet has one exact parameter? For example if I work with Exchange server I know that web-access for devices is present since 2013 version. So before this version there are no related parameters in cmdlets. Is it possible to take a cmdlet, for example New-Mailbox and check if it has one exact parameter (that parameter would not exist for 2010 version and would for 2013+)?

The question is quite old, but still.. :) Try the code below to get list of available CmdLet parameters

$params = (Get-Command New-Mailbox).ParameterSets | Select -ExpandProperty Parameters
$params | ForEach {$_.Name}

Pavel's answer is fine. This way is slightly shorter and easier to read:

(Get-Command cmdletName).Parameters['parameterName']

This example uses this to check the New-Mailbox cmdlet for the EnableRoomMailboxAccount parameter, which was added in Exchange Server 2013 (the scenario described in the question):

if((Get-Command New-Mailbox).Parameters['EnableRoomMailboxAccount']) {
    New-Mailbox -UserPrincipalName confroom1010@contoso.com `
        -Alias confroom1010 `
        -Name "Conference Room 1010" `
        -Room `
        -EnableRoomMailboxAccount $true `
        -RoomMailboxPassword (ConvertTo-SecureString -String P@ssw0rd -AsPlainText -Force)
}
else {
    New-Mailbox -UserPrincipalName confroom1010@contoso.com `
    -Alias confroom1010 `
    -Name "Conference Room 1010" `
    -Room
}

The PowerShell $args variable is an array of the parameters used in the call. You can use $args.Count to verify the desired parameter is there. You can also test against the value of the first parameter by using $args[0].

Mike

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM