Powershell 分隔输入到字符串中的值

Powershell 分隔输入到字符串中的值,powershell,parameters,parameter-passing,Powershell,Parameters,Parameter Passing,因此,我尝试创建一个Powershell菜单,当用户选择一个选项时,它将请求其尝试搜索的一个或多个值(例如Ping多台计算机)。我现在很难做到这一点。我会张贴图片来表达我的意思 当我键入一个名称进行搜索时,命令执行得很好,如下所示: 当我尝试使用多个值时,它不起作用: 以下是我的代码片段: 当然,任何帮助都是非常感谢的 更新-11/13 这就是我目前拥有的: function gadc { Param( [Parameter(Mandatory=$true)]

因此,我尝试创建一个Powershell菜单,当用户选择一个选项时,它将请求其尝试搜索的一个或多个值(例如Ping多台计算机)。我现在很难做到这一点。我会张贴图片来表达我的意思

当我键入一个名称进行搜索时,命令执行得很好,如下所示:

当我尝试使用多个值时,它不起作用:

以下是我的代码片段:

当然,任何帮助都是非常感谢的

更新-11/13

这就是我目前拥有的:

function gadc {
   Param(
       [Parameter(Mandatory=$true)]
       [string[]] $cname # Note: [string[]] (array), not [string]
       )
   $cname = "mw$cname"
   Get-ADComputer $cname
}

这是控制台中的输出

cmdlet gadc at command pipeline position 1
Supply values for the following parameters:
cname[0]: imanuel
cname[1]: troyw
cname[2]: hassan
cname[3]: 
Get-ADComputer : Cannot convert 'System.String[]' to the type 
'Microsoft.ActiveDirectory.Management.ADComputer' required by parameter 'Identity'. Specified 
method is not supported.
At line:32 char:19
+    Get-ADComputer $cname
+                   ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.G 
   etADComputer
 
Press Enter to continue...: 

**And here is the other way with the same result:**

cmdlet gadc at command pipeline position 1
Supply values for the following parameters:
cname[0]: imanuel, troyw

Get-ADComputer : Cannot convert 'System.String[]' to the type 
'Microsoft.ActiveDirectory.Management.ADComputer' required by parameter 'Identity'. Specified 
method is not supported.
At line:32 char:19
+    Get-ADComputer $cname
+                   ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.G 
   etADComputer

按Enter键继续…:

您需要将强制参数声明为数组,然后PowerShell的自动提示将允许您逐个输入多个值-提交最后一个值后只需按Enter键即可继续:

function gadc {
  param(
    [Parameter(Mandatory)]
    [string[]] $cname  # Note: [string[]] (array), not [string]
  )
  # Get-ADComputer only accepts one computer name at a time 
  # (via the positionally implied -Identity parameter), so you must loop
  # over the names.
  # The following should work too, but is slightly slower:
  #   $cname | Get-ADComputer 
  foreach ($c in $cname) { Get-ADComputer $c }
}

欢迎来到StackOverflow!请发布您的代码,而不是代码截图:)以重复前面的“发布文本”消息[grin]。。。问问题时为什么不上传代码/错误的图像Meta Stack Overflow(元堆栈溢出)-是的,在第二个屏幕截图中,当我执行命令时,它没有显示任何输出。@Lee的措辞有点含糊不清:他想说的是,你一般不应该使用图像,而应该只使用文本;图像,如果需要的话,应该只补充文本信息。@agardi-正如其他人所指出的。。。“文本图像”评论是关于不发布文本图像,除非没有其他方法来完成这项工作。我的链接显示了这个想法背后的原因。[grin]@agardi-请查看我的更新-您必须循环名称数组的元素。另外,由于我的答案是基于问题的原始形式的——这显示了不使用数组的问题,因此我建议您编辑问题以显示原始代码,以便问题和答案匹配。