Require one parameter with one switch and two with another

I have the following parameter definition. In this script, I must have the user’s access id, access key, and account name. I must also have either a device id, device name, or disply name. Finally, I require the inclusion of one or more property names and the associated value(s).

The script uses this information to add the property name and value to the device (in our monitoring system). Now, I need to add support for deleting devices, but I cannot figure out how I would change the parameter definition.

I was thinking of using a [switch] to determine if the user wants to add or delete a property. If the user included the -Add switch when running the script, then I will require both $PropertyNames and $PropertyValues. If they include -Delete, the I will only require $PropertyName.

What would be the best way to achieve this? Would it be better to have a parameter called “Operation” that will take “Add” or “Delete” and then base the PropertyName/Values requirement on the value of $Operation?

Thanks. The code snippet is below:

[CmdletBinding(DefaultParameterSetName=’Default’)] Param ( [Parameter(Mandatory=$True, ParameterSetName=’Default’)] [Parameter(Mandatory=$True, ParameterSetName=’NameFilter’)] [Parameter(Mandatory=$True, ParameterSetName=’IPFilter’)] [string]$AccessId, [Parameter(Mandatory=$True, ParameterSetName=’Default’)] [Parameter(Mandatory=$True, ParameterSetName=’NameFilter’)] [Parameter(Mandatory=$True, ParameterSetName=’IPFilter’)] [string]$AccessKey, [Parameter(Mandatory=$True, ParameterSetName=’Default’)] [Parameter(Mandatory=$True, ParameterSetName=’NameFilter’)] [Parameter(Mandatory=$True, ParameterSetName=’IPFilter’)] [string]$AccountName, [Parameter(Mandatory=$True,ParameterSetName=’Default’)] [int]$DeviceId, [Parameter(Mandatory=$True,ParameterSetName=’NameFilter’)] [string]$DeviceDisplayName, [Parameter(Mandatory=$True,ParameterSetName=’IPFilter’)] [string]$DeviceName, [Parameter(Mandatory=$True, ParameterSetName=’Default’)] [Parameter(Mandatory=$True, ParameterSetName=’NameFilter’)] [Parameter(Mandatory=$True, ParameterSetName=’IPFilter’)] [string[]]$PropertyNames, [Parameter(Mandatory=$True, ParameterSetName=’Default’)] [Parameter(Mandatory=$True, ParameterSetName=’NameFilter’)] [Parameter(Mandatory=$True, ParameterSetName=’IPFilter’)] [string[]]$PropertyValues, [switch]$WriteLog, [string]$LogPath ) 

submitted by /u/wetling
[link] [comments]

Leave a Reply