PowerShell 强制参数依赖于另一个参数 [英] PowerShell mandatory parameter depends on another parameter

查看:28
本文介绍了PowerShell 强制参数依赖于另一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更改注册表项值的 PowerShell 函数.代码:

I have a PowerShell function which changes the registry key values. Code:

param(
    [Parameter()] [switch]$CreateNewChild,
    [Parameter(Mandatory=$true)] [string]$PropertyType
)

它有一个参数CreateNewChild",如果设置了该标志,该函数将创建 key 属性,即使它没有找到.参数PropertyType"必须是强制性的,但前提是设置了CreateNewChild"标志.

It has a parameter, "CreateNewChild", and if that flag is set, the function will create the key property, even if it wasn't found. The parameter "PropertyType" must be mandatory, but only if "CreateNewChild" flag has been set.

问题是,如何使一个参数成为强制性参数,但前提是必须指定另一个参数?

The question is, how do I make a parameter mandatory, but only if another parameter has been specified?

好的,我一直在玩它.这确实有效:

OK, I've been playing around with it. And this does work:

param(
  [Parameter(ParameterSetName="one")]
  [switch]$DoNotCreateNewChild,

  [string]$KeyPath,

  [string]$Name,

  [string]$NewValue,

  [Parameter(ParameterSetName="two")]
  [switch]$CreateNewChild,

  [Parameter(ParameterSetName="two",Mandatory=$true)]
  [string]$PropertyType
)

但是,这意味着 $KeyPath、$Name 和 $NewValue 不再是强制性的.将one"参数集设置为强制会破坏代码(参数集无法解析" 错误).这些参数集令人困惑.我确定有办法,但我不知道该怎么做.

However, this means that $KeyPath, $Name and $NewValue are not mandatory any more. Setting "one" parameter set to mandatory breaks the code ("parameter set cannot be resolved" error). These parameter sets are confusing. I'm sure there is a way, but I can't figure out how to do it.

推荐答案

您可以通过定义参数集来对这些参数进行分组.

You could group those parameters by defining a parameter set to accomplish this.

param (
    [Parameter(ParameterSetName='One')][switch]$CreateNewChild,
    [Parameter(ParameterSetName='One',Mandatory=$true)][string]$PropertyType
)

参考:

https://devblogs.microsoft.com/powershell/powershell-v2-parametersets

http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/30/use-parameter-sets-to-simplify-powershell-commands.aspx

--- 更新---

这是一个模仿您正在寻找的功能的片段.额外"除非调用 -Favorite 开关,否则不会处理参数集.

Here's a snippet that mimics the functionality you're looking for. The "Extra" parameter set will not be processed unless the -Favorite switch is called.

[CmdletBinding(DefaultParametersetName='None')] 
param( 
    [Parameter(Position=0,Mandatory=$true)] [string]$Age, 
    [Parameter(Position=1,Mandatory=$true)] [string]$Sex, 
    [Parameter(Position=2,Mandatory=$true)] [string]$Location,
    [Parameter(ParameterSetName='Extra',Mandatory=$false)][switch]$Favorite,      
    [Parameter(ParameterSetName='Extra',Mandatory=$true)][string]$FavoriteCar
)

$ParamSetName = $PsCmdLet.ParameterSetName
    
Write-Output "Age: $age"
Write-Output "Sex: $sex"
Write-Output "Location: $Location"
Write-Output "Favorite: $Favorite"
Write-Output "Favorite Car: $FavoriteCar"
Write-Output "ParamSetName: $ParamSetName"

这篇关于PowerShell 强制参数依赖于另一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆