gpt4 book ai didi

powershell - 如何输入字符串列表作为参数?

转载 作者:行者123 更新时间:2023-12-03 00:33:36 27 4
gpt4 key购买 nike

我有一个 cmdlet,我在其中接受字符串列表作为参数,在 c# 类中定义,如下所示:

[Parameter(Mandatory = true)]
public List<string> AllowedScopes;

所以当我调用我的命令时, Add-Client ,如何在 PowerPhell 中提供字符串列表?我试过这个(每一行都是不同的方法):

-AllowedScopes scope1, scope2, scope3
-AllowedScopes [scope1, scope2, scope3]
-AllowedScopes {scope1, scope2, scope3}
-AllowedScopes {"scope1", "scope2", "scope3"}

但我总是在我的列表“AllowedScopes”中得到一个条目,其中包含在 AllowedScopes 参数名称之后输入的完整字符串。

我无法轻易找到有关此的任何信息,所以我想我问错了问题。

我当然可以让 AllowedScopes 参数是一个简单的字符串,然后执行以下操作:

var AllowedScopesAsList = this.AllowedScopes.Split(',').ToList();

但我认为这应该是 powershell 提供的东西(因为它提供了许多其他有用的功能,使我无法实现我的 cmdlet 的整个用户交互部分)

编辑:这是我的 cmdlet C#-class 的全部内容:

using System.Collections.Generic;
using System.Management.Automation;

namespace MyCmdlets
{
[Cmdlet("Add", "Client")]
public class AddClient : Cmdlet
{
[Parameter(Mandatory = true)]
public List<string> AllowedScopes;

protected override void ProcessRecord()
{
AllowedScopes.ForEach(a => WriteObject(a));
}
}
}

有了这个,如果我尝试像其中一个答案所说的那样输入列表:

Add-Client -AllowedScopes @('scope1', 'scope2')

我得到这个输出:
<pre>
scope1
scope2
</pre>

正如预期的那样。

但它不起作用,如果我在 PowerShell 要求时提供参数,如下所示:
<pre>
PS> <b>Add-Client</b> <kbd>Enter</kbd>

Cmdlet Add-Client at CommandPipelineposition 1
Enter the values for the following parameter:
AllowedScopes[0]: <b>@('scope1','scope2')</b> <kbd>Enter</kbd>
AllowedScopes[1]: <kbd>Enter</kbd>
</pre>

现在输出是这样的:

@('scope1','scope2')

即使我在那里一一输入范围,它仍然会导致列表只接收一个 child :
<pre>
AllowedScopes[0]: <b>'scope1'</b>
AllowedScopes[1]: <b>'scope2'</b>
AllowedScopes[2]:
</pre>

输出:
<pre>
'scope1' 'scope2'
</pre>

/编辑2:
我已经上传了一个视频,这样你就可以看到 powershell 的行为是如何不符合预期的(至少我认为不是):

Youtube-Video with powershell-misbehavior

最佳答案

您似乎点击了 bug 从 v5.1 开始影响 Windows PowerShell 和从 v6.1.0 开始影响 PowerShell Core

PowerShell 的自动提示意外地将单独输入的元素传递给绑定(bind)到参数 -AllowedScopes作为单个字符串,它是单个元素的串联,用空格分隔。
相比之下,从命令行传递一组值 - 例如,-AllowedScopes scope1, scope2, scope3 - 工作正常。

错误是由您的参数定义为 List<string> 触发的而不是 string[] .
可以说,string[]在这种情况下无论如何都是更好的选择,所以 解决方法是声明您的 -AllowedScopes参数为 string[] (数组) :

Add-Type -TypeDefinition @'
using System.Collections.Generic;
using System.Management.Automation;

namespace MyCmdlets
{
[Cmdlet("Add", "Client")]
public class AddClient : Cmdlet
{
[Parameter(Mandatory = true)]
// Use string[] instead of List<string>
public string[] AllowedScopes;

protected override void ProcessRecord()
{
foreach (var scope in AllowedScopes) {
WriteObject(scope);
}
}
}
}
'@ -PassThru | ForEach-Object Assembly | Import-Module

# Let PowerShell prompt for -AllowedScopes
# Due to use of string[] rather than List<string>, the individually entered
# elements are now correctly passed as an array.
Add-Client

请注意,如果类型不是 string,则该错误不会出现。用于通用列表。

带有一般信息的原始答案 :

您的代码按设计工作, -AllowedScopes scope1, scope2, scope3确实是传递多个列表元素的方式:
scope1, scope2, scope3使用 , ,数组构造运算符,构造一个绑定(bind)到 -AllowedScopes 的 3 元素数组。参数并转换为 List<string>实例。

PowerShell 的强制参数的自动提示机制也可以按设计工作:

对于集合类型的参数,例如 -AllowedScopes :
  • 集合的元素被单独提示,
  • 并在提交最后一个元素后再次按 Enter 表示输入结束。

  • 如果要允许用户通过单个提示传递所有元素,则必须实现自定义逻辑:
  • 声明 -AllowedScopes作为单个字符串,以便用户可以传递类似'scope1, scope2, scope3'
  • 将收到的字符串值拆分为 cmdlet 内的嵌入元素。
  • 关于powershell - 如何输入字符串列表作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52135150/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com