powershell - Allow switch parameter only when another switch is present -
i have function 2 switches, 1 being required if other present. currently, if second switch run itself, nothing happens, fine, think can better. set second parameter can present if first also. have tried making first 1 required, technically works, doesn't give result expecting (ambiguous param set error).
here param statement:
[cmdletbinding(defaultparametersetname='notransfermdrive')] param( [parameter(mandatory=$true, valuefrompipeline=$true, valuefrompipelinebypropertyname=$true)] [string]$username, [switch]$removefromallgroups, [switch]$noconfirmationprompt, [parameter(parametersetname='transfermdrive')] [switch]$transfermdrive, [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$oldserver, [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$newserver )
i modified param statement this:
[cmdletbinding(defaultparametersetname='notransfermdrive')] param( [parameter(mandatory=$true, valuefrompipeline=$true, valuefrompipelinebypropertyname=$true)] [string]$username, [parameter(parametersetname='removefromgroups',mandatory=$true)] [switch]$removefromallgroups, [parameter(parametersetname='removefromgroups')] [switch]$noconfirmationprompt, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive')] [switch]$transfermdrive, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$oldserver, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$newserver )
and prompt me value -removefromallgroups
, if leave blank, following:
do-something : cannot process argument transformation on parameter 'removefromallgroups'. cannot convert value "system.string" type "system.management.automation.switchparameter". boolean parameters accept boolean values , numbers, such $true, $false, 1 or 0. @ line:27 char:1 + do-something -noconfirmationprompt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : invaliddata: (:) [do-something], parameterbindingargumenttransformationexception + fullyqualifiederrorid : parameterargumenttransformationerror,do-something
if possible fail immediately, without prompting value -removefromallgroups
if -nopromptforconfirmation
passed itself. change make work?
normally tell you working against powershell avoiding having prompt mandatory parameters. powershell doesn't handle prompting mandatory switch parameters well. here way asking for:
function test { [cmdletbinding(defaultparametersetname='notransfermdrive')] param( [parameter(mandatory=$true, valuefrompipeline=$true, valuefrompipelinebypropertyname=$true)] [string]$username, [parameter(parametersetname='removefromgroups')] [switch]$removefromallgroups, [parameter(parametersetname='removefromgroups')] [switch]$noconfirmationprompt, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive')] [switch]$transfermdrive, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$oldserver, [parameter(parametersetname='removefromgroups')] [parameter(parametersetname='transfermdrive', mandatory=$true)] [string]$newserver ) begin { if ($noconfirmationprompt -and !$removefromallgroups) { throw "if -noconfirmationprompt specified, must specify -removeallgroups" } "username $username, removefromallgroups $removefromallgroups" } } test -username johndoe -oldserver foo -newserver bar -noconfirmationprompt
but desired behavior if -noconfirmationprompt
not passed in? -removefromallgroups
still required? perhaps not. if don't pass in switch use transfermdrive parameterset. if powershell sees -removefromallgroups
knows using removefromgroups parameterset.
Comments
Post a Comment