Powershell:读取主机到 select 数组索引
Powershell: Read-Host to select an Array Index
这是我修改 Powershell 数组的方式:
ForEach ($userID in $usersList) {
$allUsers += [pscustomobject]@{ID=$usersCounterTable;UserID=$userID;Name=$userInfo.DisplayName;Ext=$userInfo.ipPhone;Cellphone=$userInfo.mobile;Enabled=$isEnabled;VDI=$computerType;Title=$userTitle;}
$usersCounter += 1
$usersCounterTable = "[$usersCounter]"
}
稍后在代码中显示 table,我希望用户能够键入一个数字来打开该值,该数字实际上是数组 index/offset(减 1) .我不知道该怎么做。
$userID
实际上是用户的selection,因为他们还可以输入其他员工的代码进行搜索,例如搜索他的名字。我希望用户能够 select 数组索引号。
if (($userID.length -gt 0) -and ($userID.length -lt 5)) {
$indexNumber = ($userID - 1)
[????] $userFinalChoice = $allUsers[$userID].Name # NOT VALID
}
以上代码有效,如果用户输入 1 到 9999 之间的数字...
然后我想这样做:$allUsers[$userID]
($userID
是用户 select 使用 Read-Host 编辑的号码)。只是,$allUsers[$userID].Name
无效,但 $allUsers[1].Name
有效。如果我能解决这个问题,我就能修复其余部分并搜索 return 值。
还需要确保用户没有输入超出 $usersList
范围的索引(使用 $ErrorActionPreference = "SilentlyContinue"
可能会起作用,因为它只是拒绝搜索拒绝,但它不是那么干净.)
据我了解,我实际上是在寻找 $usersList.IndexOf(‘David’)
的反面,我想提供索引并获取 returned 名称。
非常感谢 - Powershell 初学者。
您向我们展示的第一个代码块确实令人困惑,因为您似乎只是从...某处获取用户详细信息,因此无法判断此信息是否确实属于同一用户。
此外,我真的不认为使用格式化的 table 作为 selection 菜单是个好主意,尤其是当列表变大时。也许您应该考虑构建一个带有列表框的表单,或者按照 Lee_Dailey 的建议使用 Out-GridView
。
无论如何,如果你想要它作为控制台菜单,首先确保ID号(真正的index
到select)以1
开头
$usersCounter = 1
# collect an array of PsCustomObjects in variable $allUsers
$allUsers = foreach ($userID in $usersList) {
# don't use $allUsers += , simply output the object
[PsCustomObject]@{
ID = "[$usersCounter]"
UserID = $userID
Name = $userInfo.DisplayName
Ext = $userInfo.ipPhone
Cellphone = $userInfo.mobile
Enabled = $isEnabled
VDI = $computerType
Title = $userTitle
}
$usersCounter++ # increment the counter
}
接下来,将其显示为 table,以便人们可以通过键入 'ID' 列中显示的数字来 select 其中一位用户。
在循环中执行此操作,因此当有人键入除有效数字以外的任何内容时,菜单会再次显示。
# start an endless loop
while ($true) {
Clear-Host
$allUsers | Format-Table -AutoSize
$userID = Read-Host "Enter the [ID] number to select a user. Type 0 or Q to quit"
if ($userID -eq '0' -or $userID -eq 'Q') { break } # exit from the loop, user quits
# test if the input is numeric and is in range
$badInput = $true
if ($userID -notmatch '\D') { # if the input does not contain an non-digit
$index = [int]$userID - 1
if ($index -ge 0 -and $index -lt $allUsers.Count) {
$badInput = $false
# everything OK, you now have the index to do something with the selected user
# for demo, just write confirmation to the console and exit the loop
Clear-Host
Write-Host "You have selected $($allUsers[$index].Name)" -ForegroundColor Green
break
}
}
# if you received bad input, show a message, wait a couple
# of seconds so the message can be read and start over
if ($badInput) {
Write-Host "Bad input received. Please type only a valid number from the [ID] column." -ForegroundColor Red
Start-Sleep -Seconds 4
}
}
这是我修改 Powershell 数组的方式:
ForEach ($userID in $usersList) {
$allUsers += [pscustomobject]@{ID=$usersCounterTable;UserID=$userID;Name=$userInfo.DisplayName;Ext=$userInfo.ipPhone;Cellphone=$userInfo.mobile;Enabled=$isEnabled;VDI=$computerType;Title=$userTitle;}
$usersCounter += 1
$usersCounterTable = "[$usersCounter]"
}
稍后在代码中显示 table,我希望用户能够键入一个数字来打开该值,该数字实际上是数组 index/offset(减 1) .我不知道该怎么做。
$userID
实际上是用户的selection,因为他们还可以输入其他员工的代码进行搜索,例如搜索他的名字。我希望用户能够 select 数组索引号。
if (($userID.length -gt 0) -and ($userID.length -lt 5)) {
$indexNumber = ($userID - 1)
[????] $userFinalChoice = $allUsers[$userID].Name # NOT VALID
}
以上代码有效,如果用户输入 1 到 9999 之间的数字...
然后我想这样做:$allUsers[$userID]
($userID
是用户 select 使用 Read-Host 编辑的号码)。只是,$allUsers[$userID].Name
无效,但 $allUsers[1].Name
有效。如果我能解决这个问题,我就能修复其余部分并搜索 return 值。
还需要确保用户没有输入超出 $usersList
范围的索引(使用 $ErrorActionPreference = "SilentlyContinue"
可能会起作用,因为它只是拒绝搜索拒绝,但它不是那么干净.)
据我了解,我实际上是在寻找 $usersList.IndexOf(‘David’)
的反面,我想提供索引并获取 returned 名称。
非常感谢 - Powershell 初学者。
您向我们展示的第一个代码块确实令人困惑,因为您似乎只是从...某处获取用户详细信息,因此无法判断此信息是否确实属于同一用户。
此外,我真的不认为使用格式化的 table 作为 selection 菜单是个好主意,尤其是当列表变大时。也许您应该考虑构建一个带有列表框的表单,或者按照 Lee_Dailey 的建议使用 Out-GridView
。
无论如何,如果你想要它作为控制台菜单,首先确保ID号(真正的index
到select)以1
$usersCounter = 1
# collect an array of PsCustomObjects in variable $allUsers
$allUsers = foreach ($userID in $usersList) {
# don't use $allUsers += , simply output the object
[PsCustomObject]@{
ID = "[$usersCounter]"
UserID = $userID
Name = $userInfo.DisplayName
Ext = $userInfo.ipPhone
Cellphone = $userInfo.mobile
Enabled = $isEnabled
VDI = $computerType
Title = $userTitle
}
$usersCounter++ # increment the counter
}
接下来,将其显示为 table,以便人们可以通过键入 'ID' 列中显示的数字来 select 其中一位用户。 在循环中执行此操作,因此当有人键入除有效数字以外的任何内容时,菜单会再次显示。
# start an endless loop
while ($true) {
Clear-Host
$allUsers | Format-Table -AutoSize
$userID = Read-Host "Enter the [ID] number to select a user. Type 0 or Q to quit"
if ($userID -eq '0' -or $userID -eq 'Q') { break } # exit from the loop, user quits
# test if the input is numeric and is in range
$badInput = $true
if ($userID -notmatch '\D') { # if the input does not contain an non-digit
$index = [int]$userID - 1
if ($index -ge 0 -and $index -lt $allUsers.Count) {
$badInput = $false
# everything OK, you now have the index to do something with the selected user
# for demo, just write confirmation to the console and exit the loop
Clear-Host
Write-Host "You have selected $($allUsers[$index].Name)" -ForegroundColor Green
break
}
}
# if you received bad input, show a message, wait a couple
# of seconds so the message can be read and start over
if ($badInput) {
Write-Host "Bad input received. Please type only a valid number from the [ID] column." -ForegroundColor Red
Start-Sleep -Seconds 4
}
}