微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

分隔输入到字符串中的值

如何解决分隔输入到字符串中的值

因此,我试图创建一个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 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?