分隔输入到字符串中的值

问题描述

因此,我试图创建一个Powershell菜单,当用户选择一个选项时,它将询问其试图搜索一个或多个值(例如Ping多台计算机)。我目前很难让它开始工作。我将发布图片显示我的意思

当我输入一个名称进行搜索时,命令执行如下:

Working with One value entered

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

Not working with multiple values

以下是我拥有的代码的简要信息:

Sample of Code

当然可以提供任何帮助。

更新-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],Microsoft.ActiveDirectory.Management.Commands.G 
   etADComputer

按Enter继续...:

解决方法

您需要将必需参数声明为 array ,然后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 }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...