我正在尝试制作一个动态终端菜单,用户可以在Powershell中从中进行选择。至此,我已经有了显示菜单的代码,但是还没有如何在变量中存储正确的值:
http://localhost:3000/api/eMail 404 (Not Found)
这将产生以下结果:
$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"
当然,当我选择 Please Select OU
1. HR
2. Accounting
3. Manufacturing
4. PR
5. Executives
Enter Option Number:
时,例如4
返回$menuSelection
而不是4
。
由于我不知道列表的内容,因为可以随时添加新的AD组织单位,所以我不确定如何使它工作。
我还想弄清楚如何添加一些逻辑,使其只包含第一列中的数字,就像输入PR
一样,它将告诉我重试直到获得数字被列出。但是,这个数字将再次随着OU数量的变化而改变。
答案 0 :(得分:0)
这是在菜单中生成可变数量的项目的一种方法。 [咧嘴]因为我无权访问AD资料,所以我使用了本地组。
$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]
输出...
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
答案 1 :(得分:0)
那怎么做...
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
答案 2 :(得分:0)
请参阅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')
执行:
================ UNIT ===== 人力资源 帐户 = PR = 出口 单位选择:人力资源 人力资源 按Enter继续...: