简体   繁体   English

无法识别PowerShell参数集

[英]PowerShell ParameterSets not being recognized

I have a powershell cmdlet, and I can't seem to get the parameter sets to work. 我有一个powershell cmdlet,但似乎无法使参数集正常工作。 This cmdlet has 31 possible parameters and it can go down two possible processing paths. 此cmdlet具有31个可能的参数,并且可以沿两条可能的处理路径进行。 All 31 parameters are optional, 18 are common to both paths, 9 are unique to the "right" path and 4 are unique to the "left" path. 所有31个参数都是可选的,两个路径共有18个,“右”路径唯一9个,“左”路径唯一4个。 What I'm trying to accomplish is to use powershell's ParameterSet attributes to specify which parameter can be used when. 我要完成的工作是使用powershell的ParameterSet属性指定何时可以使用哪个参数。 But it's not working. 但这不起作用。

Below is a mock-up of my attempt. 以下是我的尝试的模型。 Names have been changed to protect the innocent. 名称已更改以保护无辜者。

[Parameter]
public string SomeString1 { get; set; }

[Parameter]
public string SomeString2 { get; set; }

[Parameter(ParameterSetName = "left")]
public string LeftName { get; set; }

[Parameter(ParameterSetName = "left")]
public string LeftDomain { get; set; }

[Parameter(ParameterSetName = "right")]
public string RightID { get; set; }

[Parameter(ParameterSetName = "right")]
public string RightIP { get; set; }

So when I run this, I try to check my parameter lists using get-help, and all I see is one big clump of parameters when it should split them up into two lists, one including the two "left" parameters plus the two unnamed ones, and another including the "right" parameters plus the unnamed ones. 因此,当我运行此命令时,我尝试使用get-help检查我的参数列表,当将其分成两个列表时,我看到的只是一大堆参数,其中一个包括两个“左”参数以及两个未命名的参数一个,另一个包括“正确的”参数以及未命名的参数。

The hilarious part is that we have another cmdlet that lets me do this just fine, no problems, perfect output. 有趣的是,我们还有另一个cmdlet,可以让我做到这一点,而且没有问题,输出完美。 But for some reason this one isn't letting me. 但是出于某种原因,这个人没有让我。 Same includes, same syntax, just different names. 相同包括相同的语法,只是名称不同。 So I know our setup is correct, because it works sometimes. 所以我知道我们的设置是正确的,因为它有时可以工作。 But it doesn't work all of the time, so I'm stumped. 但这并不总是有效,所以我很沮丧。

I've tried changing some/all of the parameters to Mandatory, I've tried using a DefaultParameterSetName at the header. 我尝试将某些/所有参数更改为强制性,我尝试在标头使用DefaultParameterSetName。 I've tried doubling up on sets, like including a set named "both" and making every parameter part of that set. 我尝试将集合加倍,例如包含一个名为“ both”的集合,并使每个参数成为该集合的一部分。 I've read through the MSDN article on parameter sets, I've read through the first 40 articles that google returns, but I can't get it working. 我已经阅读了有关参数集的MSDN文章,也已经阅读了Google返回的前40篇文章,但是我无法使其正常运行。 Half of my team has stood behind my desk watching me do it, and they're all stumped as well. 我的团队中有一半站在我的办公桌后面看着我这样做,他们也都陷入了困境。

What can I do to: 我该怎么做:

  • figure out why it isn't recognizing my parameter sets 弄清楚为什么它无法识别我的参数集
  • make it so that it does 做到这一点
  • prevent this from happening in the future 防止将来发生这种情况

Keith Hill, thanks for the tip - I tried commenting out all but the most minimal parameters and I noticed that when I ran the get-help on the cmdlet that it still returned all of the parameters . 基思·希尔(Keith Hill),谢谢您的提示-我尝试注释掉了除最基本参数以外的所有参数,并且我注意到当我在cmdlet上运行get-help时,它仍然返回所有参数 I knew something goofy was going on, and it turns out that the help file was not getting updated when I compiled the cmdlet. 我知道发生了一些问题,事实证明,在编译cmdlet时,帮助文件没有得到更新。

When I worked that issue out, it appears that everything was functioning exactly as I expected it to, it was that the get-help was using old information. 当我解决该问题时,似乎所有功能都按我预期的那样正常运行,这是因为获取帮助正在使用旧信息。 That's why nobody else could figure it out, either. 这就是为什么没人能弄清楚的原因。 I was doing it right, so there were no bugs, just poor documentation. 我做得对,所以没有错误,只有糟糕的文档。

Major facepalm moment, but I got it worked out. 主要的facepalm时刻,但我知道了。 The lesson is: always make sure your help is automatically generating. 课程是:始终确保自动生成您的帮助。

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

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