简体   繁体   English

Powershell - 用文本文件制作菜单

[英]Powershell - Make a menu out of text file

In my adventure trying to learn Powershell, I am working on an extension on a script I have made.在我尝试学习 Powershell 的冒险中,我正在对我制作的脚本进行扩展。 The idea is to make script there by adding ".iso" files into a folder.这个想法是通过将“.iso”文件添加到文件夹中来制作脚本。 It will use that content in a menu so that I later can use it to select an iso file for a WM in Hyper-V它将在菜单中使用该内容,以便我以后可以使用它为 Hyper-V 中的 WM 选择一个iso文件

This is my version of how it will get the content in the first place这是我如何首先获取内容的版本

Get-ChildItem -Path C:\iso/*.iso -Name > C:\iso/nummer-temp.txt
Add-Content -Path C:\iso/nummer.txt ""
Get-Content -Path C:\iso/nummer-temp.txt | Add-Content -Path C:\iso/nummer.txt

When this code is run it will send an output like what i want.运行此代码时,它将发送我想要的输出。 But my question is how do I use this output in a menu?但我的问题是如何在菜单中使用此输出?

This is the best practice way to do so in powershell :这是在 powershell 中执行此操作的最佳实践方法:

#lets say your .txt files gets this list after running get-content



$my_isos = $('win7.iso','win8.iso','win10.iso')

$user_choice = $my_isos | Out-GridView -Title 'Select the ISO File you want'  -PassThru

#waiting till you choose the item you want from the grid view
Write-Host "$user_choice is going to be the VM"

I wouldn't try to make it with System.windows.forms utilities as i mentioned in my comment, unless you want to present the form more "good looking".我不会像我在评论中提到的那样尝试使用 System.windows.forms 实用程序来实现它,除非您想呈现更“好看”的表单。

If you don't want to go for a graphical menu, but rather a console menu, you could use this function below:如果您不想使用图形菜单,而是使用控制台菜单,则可以使用以下功能:

function Show-Menu {
    Param(
        [Parameter(Position=0, Mandatory=$True)]
        [string[]]$MenuItems,
        [string] $Title
    )

    $header = $null
    if (![string]::IsNullOrWhiteSpace($Title)) {
        $len = [math]::Max(($MenuItems | Measure-Object -Maximum -Property Length).Maximum, $Title.Length)
        $header = '{0}{1}{2}' -f $Title, [Environment]::NewLine, ('-' * $len)
    }

    # possible choices: digits 1 to 9, characters A to Z
    $choices = (49..57) + (65..90) | ForEach-Object { [char]$_ }
    $i = 0
    $items = ($MenuItems | ForEach-Object { '{0}  {1}' -f $choices[$i++], $_ }) -join [Environment]::NewLine

    # display the menu and return the chosen option
    while ($true) {
        cls
        if ($header) { Write-Host $header -ForegroundColor Yellow }
        Write-Host $items
        Write-Host

        $answer = (Read-Host -Prompt 'Please make your choice').ToUpper()
        $index  = $choices.IndexOf($answer[0])

        if ($index -ge 0 -and $index -lt $MenuItems.Count) {
            return $MenuItems[$index]
        }
        else {
            Write-Warning "Invalid choice.. Please try again."
            Start-Sleep -Seconds 2
        }
    }
}

Having that in place, you call it like:有了它,你称之为:

# get a list if iso files (file names for the menu and full path names for later handling)
$isoFiles = Get-ChildItem -Path 'D:\IsoFiles' -Filter '*.iso' -File | Select-Object Name, FullName
$selected = Show-Menu -MenuItems $isoFiles.Name -Title 'Please select the ISO file to use'
# get the full path name for the chosen file from the $isoFiles array
$isoToUse = ($isoFiles | Where-Object { $_.Name -eq $selected }).FullName

Write-Host "`r`nYou have selected file '$isoToUse'"

Example:例子:

 Please select the ISO file to use --------------------------------- 1 Win10.iso 2 Win7.iso 3 Win8.iso Please make your choice: 3 You have selected file 'D:\\IsoFiles\\Win8.iso'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM