【发布时间】:2019-10-07 20:33:50
【问题描述】:
我正在创建一个允许批量创建新用户的脚本,但在创建数组时遇到了问题。
$fname = @()
$lname = @()
$i = 0
$fname[$i] = Read-Host "`nWhat is the first name of the new user?"
$fname[$i] = $fname[$i].trim()
$lname[$i] = Read-Host "What is the last name of the new user?"
$lname[$i] = $lname[$i].trim()
如果我运行它,我会收到错误:
Index was outside the bounds of the array.
At line:1 char:1
+ $fname[$i] = Read-Host "`nWhat is the first name of the new user?"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException
Method invocation failed because [System.Object[]] does not contain a method named 'trim'.
At line:2 char:13
+ $fname[$i] = $fname.trim()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
【问题讨论】:
-
如果接受的链接问题的答案不能解决您的问题,请告诉我们。
-
或者类似这样的:
$names = 1..2 | foreach { [pscustomobject]@{fname = read-host first; lname = read-host last } }
标签: powershell