如何解决分隔输入到字符串中的值
因此,我试图创建一个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],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 举报,一经查实,本站将立刻删除。