在Powershell中根据变量创建显示菜单 [英] Create show-menu in powershell depending on variables
本文介绍了在Powershell中根据变量创建显示菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个带有显示菜单的菜单,该菜单取决于计算机上显示的内容.
I want to create a menu with show-menu that will depend on what is present on the computer.
我想在菜单中列出c:\ users \上可用的用户名(基于文件夹名称).
I want to list in the menu usernames available on c:\users\ (based on the folders names).
例如: 在c:\ users中 没有命名的文件夹:
For example : in c:\users there is folder nammed :
homer.simpson
lisa.simpson
bart.simpson
并带有显示菜单,要求用户通过为homer.simpson键入1或为lisa.simpson等键入2来选择其中之一.
and with show-menu ask the user to choose one of them by typing 1 for homer.simpson 2 for lisa.simpson etc ..
我该怎么办?
提前谢谢!
代码
$users = Get-ChildItem "$env:SystemDrive\Users"| ForEach-Object { $_.Name }
foreach ($user in $user) {
$user
$num++
New-Variable -Name "a$num" -Value $user
#Get-Variable -Name "$user$i"
}
function Show-Menu
{
param (
[string]$Title = 'Please select an user'
)
cls
Write-Host "================ $Title ================"
Write-Host "1: $a1"
Write-Host "2: $a2"
Write-Host "3: $a3"
Write-Host "Q: $a4"
}
do
{
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input)
{
'1' {
cls
'You chose option #1'
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
推荐答案
查看全文