Making a dynamic menu in Powershell

风格不统一 提交于 2021-01-28 21:11:40

问题


I'm trying to make a dynamic terminal menu that a user can select from in Powershell. At this point I have the code to display the menu, but not how to store the correct value in a variable:

$ouCounter=1
Write-Host "`n Please Select OU"
foreach ($ouEntry in ((Get-ADOrganizationalUnit -LDAPFilter '(name=*)' - 
SearchBase 'OU=Resources,DC=dept,DC=ad,DC=domain,DC=tld' -SearchScope OneLevel).Name)){ 
Write-Host $("   "+$ouCounter+".`t"+$ouEntry) 
$ouCounter++ 
} 
[int]$menuSelection = Read-Host "`n Enter Option Number"

This will produce this:

 Please Select OU 

   1.   HR 
   2.   Accounting 
   3.   Manufacturing 
   4.   PR 
   5.   Executives 

 Enter Option Number:

Of course when I select 4 for example $menuSelection returns 4 and not PR.

And since I don't know what the content of the list will be, because new AD Organizational Units can be added at any time, I'm not sure how to make this work.

I'd also like to figure out how to add some logic that it would only except the numbers in the first column, as in if I entered 6 it would tell me to try again until it got a number that was listed. But, again that number will change with the amount of OUs present.


回答1:


here is one way to generate a variable number of items in a menu. [grin] since i have no access to the AD stuff, i used my local groups.

$LocalGroupList = Get-LocalGroup -Name 'a*', 'h*', 'p*', 'u*'

foreach ($MenuItem in $LocalGroupList)
    {
    '{0} - {1}' -f ($LocalGroupList.IndexOf($MenuItem) + 1), $MenuItem.Name
    }

$Choice = ''
while ([string]::IsNullOrEmpty($Choice))
    {
    Write-Host
    $Choice = Read-Host 'Please choose an item by number '
    if ($Choice -notin 1..$LocalGroupList.Count)
        {
        [console]::Beep(1000, 300)
        Write-Warning ''
        Write-Warning ('    Your choice [ {0} ] is not valid.' -f $Choice)
        Write-Warning ('        The valid choices are 1 thru {0}.' -f $LocalGroupList.Count)
        Write-Warning '        Please try again ...'
        pause

        $Choice = ''
        }
    }

''
'You chose {0}' -f $LocalGroupList[$Choice - 1]

output ...

1 - Administrators
2 - HomeUsers
3 - Homonymic_Tuu
4 - Performance Log Users
5 - Performance Monitor Users
6 - Power Users
7 - Users

Please choose an item by number : 44
WARNING: 
WARNING:     Your choice [ 44 ] is not valid.
WARNING:         The valid choices are 1 thru 7.
WARNING:         Please try again ...
Press Enter to continue...: 

Please choose an item by number : 2

You chose HomeUsers



回答2:


How about just doing this...

Clear-Host

$ouCounter = 1
$MenuArray = @()

Write-Host "`n Please Select OU"

$DomainName = ($env:USERDNSDOMAIN).split('.')[0]
$Tld = ($env:USERDNSDOMAIN).split('.')[1]


foreach ($ouEntry in ((Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase "DC=$DomainName,DC=$Tld" -SearchScope OneLevel).Name))
{ 
    $("   "+$ouCounter+".`t"+$ouEntry) 
    $ouCounter++ 
    $MenuArray += ,$ouEntry
}

do
{ [int]$menuSelection = Read-Host "`n Enter Option Number" }
until ([int]$menuSelection -le $ouCounter -1)

$MenuArray[$menuSelection -1]


# Results

 Please Select OU
   1.   Domain Controllers
   2.   EnterpriseServiceAccounts
   ...
   8.   FCI Server Security Groups

 Enter Option Number: 9

 Enter Option Number: 10

 Enter Option Number: 11

 Enter Option Number: 12

 Enter Option Number: 1
Domain Controllers



回答3:


Please refer https://www.business.com/articles/powershell-interactive-menu/

function Menu
{
     cls

     Write-Host "================ UNIT====="
     Write-Host "HR"
     Write-Host "Account"
     Write-Host "=PR="
     Write-Host "EXIT"
}

#Loop

do
{
     Menu
     #
       $Value = Read-Host "UNIT selection"
     switch ($Value)
     {
           'HR' {

                Write-Host "Human resource"
                #Execute the Command 
           } 'Account'

                { 

                #Execute command
           } 'PR' {

                #EXECUTE Command
                echo "test pr"
           } 'q' {
           cls
                return
           }
     }
     Sleep 2
     pause
}
until ($input -eq 'q')

Execution :

================ UNIT===== HR Account =PR= EXIT UNIT selection: HR Human resource Press Enter to continue...:



来源:https://stackoverflow.com/questions/55152044/making-a-dynamic-menu-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!