【问题标题】:Powershell: Read-Host to select an Array IndexPowershell:读取主机以选择数组索引
【发布时间】:2021-02-03 19:36:26
【问题描述】:

这就是我修改我的 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]"
}

稍后在代码中显示表格,我希望用户能够键入一个数字来打开该值,该数字实际上是数组索引/偏移量(减 1)。我不知道该怎么做。

$userID实际上是用户的选择,因为他们也可以输入另一个员工的代码来搜索,或者搜索他的名字。我希望用户能够选择数组索引号。

if (($userID.length -gt 0) -and ($userID.length -lt 5)) {
    $indexNumber = ($userID - 1)
    [????]  $userFinalChoice = $allUsers[$userID].Name  # NOT VALID
}

如果用户输入一个介于 1 和 9999 之间的数字,则上述代码有效... 然后我想这样做:$allUsers[$userID]$userID 是用户使用 Read-Host 选择的数字)。只是,$allUsers[$userID].Name 无效,但 $allUsers[1].Name 有效。如果我能解决这个问题,我将能够修复它的其余部分并搜索返回值。

还需要确保用户没有输入超出$usersList 范围的索引(使用$ErrorActionPreference = "SilentlyContinue" 可能会起作用,因为它只是拒绝搜索拒绝,但它不是那么干净。)

据我了解,我实际上是在寻找 $usersList.IndexOf(‘David’) 的反面,我想提供索引并返回名称。

非常感谢 - Powershell 初学者。

【问题讨论】:

  • 由于您没有显示您正在使用的实际代码,这只是一个 WAG ...Read-Host 返回一个[string]。您可能需要强制用户输入为[int]
  • 您的列表中有多少项目?如果总数相当低,您可以使用Out-GridView 让此人选择要处理的项目。
  • 从来不知道Out-GridView但是很有趣(应用的重点是速度,用鼠标选择它会导致操作太长),即使我想通了,我什至无法得到硬编码的代码=((糟糕)我制作循环的方式,我会尝试找出答案。ReadHost 将接受用户标识符(ed12167),例如用户名 Eric Dan 或位置 6 位数字(650657)列出此位置内的用户。现在,我们的想法是能够在此位置内选择一个用户,而无需键入 ed12167 或“Eric D”(将结果限制为 1 个匹配以打开用户信息)
  • 本以为是一件容易的事我显然错过了,显然不是那么容易呵呵
  • 我无法访问 AD 的东西,所以我没有想法。我会回去潜伏......祝你好运! [咧嘴]

标签: arrays powershell indexing offset read-host


【解决方案1】:

您向我们展示的第一个代码块确实令人困惑,因为您似乎只是从……某处获取用户详细信息,因此无法判断此信息是否确实属于同一用户。

另外,我真的不认为使用格式化表格作为选择菜单是一个好主意,尤其是在列表变大的情况下。也许您应该考虑构建一个带有列表框的表单,或者按照 Lee_Dailey 的建议使用 Out-GridView

无论如何,如果您希望它作为控制台菜单,首先确保 ID 号(实际上是要选择的 index)以 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
}

接下来,将此显示为表格,以便人们可以通过键入“ID”列中显示的数字来选择其中一个用户。 循环执行此操作,因此当有人键入有效数字以外的任何内容时,菜单会再次显示。

# 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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多