2 Replies Latest reply on May 17, 2019 8:49 AM by eaphilipp

    Dynamic Menu Help

    eaphilipp Enthusiast

      I am trying to build a script that populates a menu using a get command. I have everything working up until the part where when I make my menu selection, I need that menu item to be used in the next command. In this script I am getting VM tag names using:

       

      Get-TagCategory -Name BackUps | Get-Tag | select name | Out-File C:\VMWare-TXTfiles\VeeamTags.txt -force

       

      I then get the content of the file and populate the menu:

       

      $v = get-content 'C:\VMWare-TXTfiles\VeeamTags.txt' | select-object -skip 3

      $list = @($v)

       

      # Create blank array to hold menu

      $formattedList = @()

       

       

      # Even Odd Columns

      for($i=0;$i -lt $list.Count; $i+=2) {

          # Check if even exists

          if ($list[$i+1] -ne $null) {

              $formattedList += [PSCustomObject]@{

                  Odd = "$($i+1). $($list[$i])";

                  Even = "$($i+2). $($list[$i+1])"

              }

          }

          else {

              $formattedList += [PSCustomObject]@{

                  Odd = "$($i+1). $($list[$i])";

                  Even = ""

              }

          } 

      }

       

       

      # Output menu

      $formattedList | Format-Table -HideTableHeaders

       

       

      So the menu looks like this, it is 86 items in total but you get the point:

       

      1. MGMT-0330          2. STRETCHWLD01-1000

      3. CLU01-2330         4. STRETCHWLD01-1530

       

       

      And I get prompted to choose a tag, When I choose one of the tags. I need it to populate the command: get-vm -tag ""

       

       

      So if I choose 1 it would do the following: get-vm -tag MGMT-0330

      I have this as my next line.

       

       

      Switch ($input){

      '1' {get-vm -tag <tag name from menu>}

       

       

      I am guessing I need to some how change that 1 to a variable and whats inside the tag name from menu to something? Anyone ever done and thing like this? Thanks for the help...

        • 1. Re: Dynamic Menu Help
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          I'm not sure why write/read via that file?
          And I don't think you need a switch-block.
          Try something like this

           

          $v = Get-TagCategory -Name Backups | Get-Tag

          $menu = for ($i = 0; $i -lt $v.Count; $i += 2)

          {

             $obj = [ordered]@{

             Odd = "$($i+1). $($v[$i].Name)"

             }

             if ($v[$i + 1])

             {

             $obj.Add('Even', "$($i+2). $($v[$i+1].Name)")

             }

             New-Object PSObject -Property $obj

          }

          $menu | Format-Table -HideTableHeaders

          $choice = Read-Host -Prompt "Select tag (1..$($v.Count))"


          Get-VM -Tag $v[$choice - 1].Name

          • 2. Re: Dynamic Menu Help
            eaphilipp Enthusiast

            AWESOME! Yes thank you this works great. I tried to do it my way because I wasn't sure of the right way! Thanks very much for the help.