none
Powershell select menu RRS feed

  • Question

  • Greeting,

    I have a text file with all the Older machine's information as below

    machine name      RAM     HD     OS

    ----------------        -------    ----     -------

    win7                     4gb     40GB   windows 7

    windows2            8gb     120gb   windows 2012 R2

    ..

    ...

    Windows2016     16gb    120gb    Windows 2016

    what I am looking for is, use the existing information to create a select menu like

    Menu
    ====


    1. win7                     4gb     40GB   windows 7

    2. windows2            8gb     120gb   windows 2012 R2

    3. ...

    4.  ...

    18. Windows2016     16gb    120gb    Windows 2016


    Select the number[1..18] to remove the machine from domain:


    then I just type "1" to remove win7, "2" to remove windows2..... "18" to remove windows2016 from domain

    I did use get-content to get the existing file information, and have code ready for remove the machine from domain.

    the point is how to link them together to make a select menu (like old DOS time)

    Thanks and Best Regards,
    NewBie in Powershell

    • Moved by Bill_Stewart Friday, March 9, 2018 7:16 PM Abandoned
    Wednesday, June 28, 2017 1:23 PM

All replies

  • Show your code!

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Wednesday, June 28, 2017 1:47 PM
  • Thanks for reply, code as below.

    Thanks for help

    newbie in powershell

    $file="c:\temp\file.txt"
    Write-host "Select menu"
    Write-host "==========="
    write-host " "

    $lines=get-content $file
     
     $i=1

     foreach ($line in $lines)
      {
      Write-host $i "." $line
      $i=$i+1

      }

    $realloop=[int]$i-1
     do
    {
        write-host " "
        $selection = Read-Host "Please make a selection:[1..$realloop], 'Q' to quit program"
        switch ($selection)
        {
            '1' {
                'You chose option #1'
            } '2' {
                'You chose option #2'
            } '3' {
                'You chose option #3'
            }
        }
    }
    until ($selection -eq 'q')

    content in file: c:\temp\file.txt

    win7        4gb     40GB   windows 7
    windows2    8gb     120gb  windows 2012 R2
    Windows2016 16gb    120gb  Windows 2016

    Wednesday, June 28, 2017 2:06 PM
  • OK. I recommend that you'd rather use a csv file than a plain text file. Assuming you do this you could start with something like this:

    That's your "information file":

    machinename,RAM,HD,OS
    win7,4gb,40GB,windows 7
    windows2,8gb,120gb,windows 2012 R2
    Windows2016,16gb,120gb,Windows 2016

    And that's how you use it do display the content including an index number: 

    Clear-Host
    $Content = Import-Csv -Path 'C:\sample\file.csv' -Delimiter ','
    Write-host "`n`tSelect menu:"
    
    $Index = 1
    $List = ForEach($Line in $Content){
        [PSCustomObject]@{
            Index = $Index++
            MachineName = $Line.machinename
            RAM = $Line.RAM
            HDD = $Line.HD
            OperatingSystem = $Line.OS
        }
    } 
    $List | ft -a
    Of course that'd be an inspiration. You can extend this just as you like it.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, June 28, 2017 2:59 PM
    Wednesday, June 28, 2017 2:58 PM
  • Thank you very very for the help.

    I think my point is:

    once the menu created, and user selected, the next step is I need to get what the user selected

    for example,

    if user select "2", then I need to get the machinename is "windows2", and then I can go next step to remove it from the Domain.

    I think may be I really need the Array to work on it

    thanks

    Newbie in Powershell

    Wednesday, June 28, 2017 4:44 PM
  • Thanks for help, Array fixed my problem

    Again, thanks

    newbie in powershell

    Wednesday, June 28, 2017 5:52 PM
  • Ah ... now I know what you meant. If you have the index number the user choose you can simply access the proper property with its index. Try it this way:
    Clear-Host
    $content = Import-Csv -Path C:\sample\file.csv -Delimiter ','
    Write-host "`n`tSelect menu:"
    
    $Index = 1
    $List = ForEach($Line in $Content){
        [PSCustomObject]@{
            Index = $Index++
            MachineName = $Line.machinename
            RAM = $Line.RAM
            HDD = $Line.HD
            OperatingSystem = $Line.OS
        }
    } 
    $List | ft -a
    
     do
    {
        write-host " "
        $selection = Read-Host "Please make a selection:[1..$($List.Count)], 'Q' to quit program"
        switch ($selection)
        {
            '1' {
                    'You chose option #' +$selection
                    'MachineName: ' + $content[$selection -1 ].machinename 
                }
            '2' {
                    'You chose option #' +$selection
                    'MachineName: ' + $content[$selection -1 ].machinename 
                }
            '3' {
                    'You chose option #' +$selection
                    'MachineName: ' + $content[$selection -1 ].machinename 
                }
        }
    }
    until ($selection -eq 'q')


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by I.T Delinquent Thursday, June 29, 2017 7:59 AM
    Wednesday, June 28, 2017 6:01 PM