简体   繁体   English

如何确定是否指定了PowerShell Cmdlet参数值?

[英]How do I determine if a PowerShell Cmdlet parameter value was specified?

In PowerShell 1.0, if I have a cmdlet parameter of an enum type, what is the recommended method for testing whether the user specified that parameter on the cmdlet command line? 在PowerShell 1.0中,如果我有一个枚举类型的cmdlet参数,那么测试用户是否在cmdlet命令行上指定该参数的推荐方法是什么? For example: 例如:

MyEnum : int { No = 0, Yes = 1, MaybeSo = 2 }

class DoSomethingCommand : PSCmdlet
...
private MyEnum isEnabled;

[Parameter(Mandatory = false)]
public MyEnum IsEnabled
{
    get { return isEnabled; }
    set { isEnabled = value; }
}

protected override void ProcessRecord()
{
    // How do I know if the user passed -IsEnabled <value> to the cmdlet?
}

Is there any way to do this without having to seed isEnabled with a dummy value? 有没有办法做到这一点,而不必使用虚拟值种子isEnabled? By default it will equal 0, and I don't want to have to seed every parameter or add a dummy value to my enum. 默认情况下,它将等于0,我不希望为每个参数设置种子或为我的枚举添加虚拟值。 I've potentially got many cmdlets with 100's of parameters, there's got to be a better way. 我可能有很多带有100个参数的cmdlet,必须有更好的方法。 This is related to this question but I was looking for a cleaner way of doing this. 这与这个问题有关,但我一直在寻找一种更清洁的方法。 Thanks. 谢谢。

In this case, I would use a nullable wrapper around the enum type eg 在这种情况下,我会在枚举类型周围使用可空的包装器,例如

[Parameter(Mandatory = false)]
public MyEnum? IsEnabled { get; set; }

Note the ? 注意? modifier on MyEnum. MyEnum上的修饰符。 Then you can test if it is set like so: 然后你可以测试它是否设置如下:

if (this.IsEnabled.HasValue) { ... }

PowerShell aside, it's never good style to use 0 in that way. 除了PowerShell之外,以这种方式使用0永远不是好的风格。 You should always use 0 for the most appropriate default. 您应始终使用0作为最合适的默认值。 In this case, the most appropriate default should be something like "Unset." 在这种情况下,最合适的默认值应该是“Unset”。

Ultimately, this is nothing to do with PowerShell and everything to do with good coding practices for .NET. 最终,这与PowerShell无关,而且与.NET的良好编码实践有关。

  • Oisin [287]莪

As an option you can use $PSBoundParameters collection to check whether passed to the method. 作为选项,您可以使用$PSBoundParameters集合来检查是否传递给该方法。

Let's suppose we have a json file like following { "p1": "value1", "p2": "value2" } and we want to create a function which accepts parameter p1 and p2 and update value of p1 and p2 in the file, if they are passed to the function. 假设我们有一个json文件,如下面的{ "p1": "value1", "p2": "value2" } ,我们想创建一个接受参数p1p2的函数,并更新文件中p1p2值,如果它们被传递给函数。 Let's say those values can be null and and having those values as null is not equivalent to not passing them. 假设这些值可以为null ,并且将这些值设为null并不等同于不传递它们。

For example Update-Values -p1 $null should update p1 to null and should not change p2 . 例如, Update-Values -p1 $null应将p1更新为null ,不应更改p2

To do so, we should be able to detect if the parameter has been passed to the method or not. 为此,我们应该能够检测参数是否已传递给方法。

Example - How to detect if a parameter is passed, for an optional parameter which can accept null as value? 示例 - 如何检测是否传递参数,对于可以接受null作为值的可选参数?

Function Update-Values ($p1, $p2) {
    If($PSBoundParameters.ContainsKey("p1")) {
        Write-Host "p1 passed"
    }
    else {
        Write-Host "p1 not passed"
    }
    If($PSBoundParameters.ContainsKey("p2")) {
        Write-Host "p2 passed"
    }
    else {
        Write-Host "p2 not passed"
    }
}

Then if you run the function using the following parameter: 然后,如果使用以下参数运行该函数:

Update-Values -p1 $null
# Update-Values -p1 "something"

As a result you will see: 结果你会看到:

p1 passed
p2 not passed

You can read the blog post here: How to determine if a parameter is passed to a Powershell Cmdlet . 您可以在此处阅读博客文章: 如何确定参数是否传递给Powershell Cmdlet

I know this thread is a little old now, but the best way to do this is to declare your parameter using the SwitchParameter type. 我知道这个线程现在有点老了,但最好的方法是使用SwitchParameter类型声明你的参数。 Then your users don't have to pass -IsEnabled , they would just add something like -Enabled as a parameter. 然后你的用户不必传递-IsEnabled,他们只需添加像-Enabled这样的参数。

You then test the .IsPresent property of your parameter to see if the caller added -Enabled to the call to the cmdlet. 然后,测试参数的.IsPresent属性,以查看调用者是否在对cmdlet的调用中添加了-Enabled。

Only thing I can see is to modify your enum so that value 0 is named Unknown or something like that. 我唯一能看到的是修改你的枚举,以便将值0命名为Unknown或类似的东西。

Problem is that enums are just integers in background and integers are value types. 问题是枚举只是背景中的整数而整数是值类型。 Unfortunate consequence is that they need to have value and that value is 0 by default. 不幸的后果是它们需要有值,默认值为0。

bool wasSpecified = MyInvocation.BoundParameters.ContainsKey("IsEnabled");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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