Guest User

Select Item From List

a guest
Oct 19th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <#
  2.         .SYNOPSIS
  3.             Select-ItemFromList utility receives a collection of objects and will  
  4.             display the list as either a numbered text list as a menu or in a list  
  5.             box for selection. There is a switch to enable multiple item selection.
  6.         .DESCRIPTION
  7.             The utility takes a collection of objects and builds a list for displaying  
  8.             the selections.  You can specify the property of to use for the display  
  9.             list.  If no property is specified the ToString() method of the object will
  10.             be used.  Often this simply displays the object's type.
  11.         .PARAMETER  options
  12.             options is the collection of objects to be displayed.
  13.         .PARAMETER  displayProperty
  14.             displayProperty defines what property of the object to use in the list
  15.             for selection.
  16.         .PARAMETER  title
  17.             title defines the heading on the form used to for displaying the list box
  18.             or the text displayed before the menu listing.  The default is "Select Item"
  19.         .PARAMETER  mode
  20.             mode specifies whether to use a ListBox, CheckedListBox or a text listing to  
  21.             display the options.  When using a menu format the items are prefixed with a  
  22.             number to identify the options.  
  23.                 The options are:
  24.                     Menu            A numbered text list
  25.                     ListBox         A ListBox of the items, the behaviour can be modified
  26.                                     with the selectMultiple switch.
  27.                     CheckedListBox  A CheckedListBox of the items
  28.                      
  29.                     The default is ListBox
  30.         .PARAMETER  selectionMode
  31.             selectionMode controls how items will be selected.  This parameter is only
  32.             used by Menu and ListBox modes
  33.             Options are:
  34.                 None
  35.                 One
  36.                 MultiSimple
  37.                 MultiExtended
  38.         .EXAMPLE
  39.             Select-ItemFromList -options (Get-Process) -displayProperty ProcessName -mode ListBox -selectMultiple
  40.         .EXAMPLE
  41.             Select-ItemFromList -options (Get-Process) -displayProperty ProcessName -mode CheckedListBox
  42.         .EXAMPLE
  43.             Select-ItemFromList -options "One","Two","Three" -mode Menu -selectMultiple
  44.         .OUTPUTS
  45.             The objects from the input options that were selected are outputted to the
  46.             pipeline.
  47.     #>
  48.     Add-Type -AssemblyName "System.Windows.Forms"
  49.     function Select-ItemFromList
  50.     {
  51.     [CmdletBinding()]
  52.     PARAM  
  53.     (
  54.         [Parameter(Mandatory=$true)]
  55.         $options,
  56.         [string]$displayProperty,
  57.         [string]$title = "Select Item",
  58.         [ValidateSet("Menu","ListBox","CheckedListBox")]
  59.         [string]$mode = "ListBox",
  60.         [System.Windows.Forms.SelectionMode]$selectionMode = [System.Windows.Forms.SelectionMode]::One
  61.     )
  62.         $script:selectedItem = $null
  63.         $selectMultiple = ($selectionMode -eq [System.Windows.Forms.SelectionMode]::MultiSimple -or $selectionMode -eq [System.Windows.Forms.SelectionMode]::MultiExtended)
  64.         [Windows.Forms.form]$form = new-object Windows.Forms.form
  65.          
  66.         switch($mode.ToLower())  
  67.         {
  68.             "checkedlistbox"
  69.             {
  70.                 [System.Windows.Forms.CheckedListBox]$lstOptions = New-Object System.Windows.Forms.CheckedListBox  
  71.                 $lstOptions.CheckOnClick = $True
  72.             }
  73.             "listbox"
  74.             {
  75.                 [System.Windows.Forms.ListBox]$lstOptions = New-Object System.Windows.Forms.ListBox
  76.                 $lstOptions.SelectionMode  = $selectionMode
  77.             }
  78.         }
  79.          
  80.         function processOK
  81.         {
  82.             $script:selectedItem = $null
  83.             if ($mode.ToLower() -eq "checkedlistbox")
  84.             {
  85.                 if ($lstOptions.CheckedIndices.Count -gt 0)
  86.                 {
  87.                     $script:selectedItem = @($options[$lstOptions.CheckedIndices[0]])
  88.                     for ($i = 1;$i -lt $lstOptions.CheckedIndices.Count;$i++)
  89.                     {
  90.                         $script:selectedItem += $options[$lstOptions.CheckedIndices[$i]]
  91.                     }
  92.                 }
  93.             }
  94.             if ($mode.ToLower() -eq "listbox")
  95.             {
  96.                 if ($lstOptions.SelectedIndices.Count -gt 0)
  97.                 {
  98.                     $script:selectedItem = @($options[$lstOptions.SelectedIndices[0]])
  99.                     for ($i = 1;$i -lt $lstOptions.SelectedIndices.Count;$i++)
  100.                     {
  101.                         $script:selectedItem += $options[$lstOptions.SelectedIndices[$i]]
  102.                     }
  103.                 }
  104.             }
  105.                 $form.Close()
  106.         }
  107.      
  108.         function processCancel
  109.         {
  110.             $script:selectedItem = $null
  111.             $form.Close()
  112.         }
  113.      
  114.      
  115.         function BuildMenu
  116.         {
  117.         PARAM  
  118.         (
  119.             [Parameter(Mandatory=$true)]
  120.             $options,
  121.             [string]$displayProperty,
  122.             [string]$title = "Select Item"
  123.         )
  124.             [int]$optionPrefix = 1
  125.             $selectMultiple = ($selectionMode -eq [System.Windows.Forms.SelectionMode]::MultiSimple -or $selectionMode -eq [System.Windows.Forms.SelectionMode]::MultiExtended)
  126.             [System.Text.StringBuilder]$sb = New-Object System.Text.StringBuilder
  127.             $sb.Append([Environment]::NewLine + $title + [Environment]::NewLine + [Environment]::NewLine) | Out-Null
  128.              
  129.             foreach ($option in $options)
  130.             {
  131.                 if ([String]::IsNullOrEmpty($displayProperty))
  132.                 {
  133.                     $sb.Append(("{0,3}: {1}" -f $optionPrefix,$option) + [Environment]::NewLine) | Out-Null
  134.                 }
  135.                 else
  136.                 {
  137.                     $sb.Append(("{0,3}: {1}" -f $optionPrefix,$option.$displayProperty) + [Environment]::NewLine) | Out-Null
  138.                 }
  139.                 $optionPrefix++
  140.             }
  141.             $sb.Append([Environment]::NewLine) | Out-Null
  142.             if ($selectMultiple)
  143.             {
  144.                 $sb.Append("Make multiple selections as a comma delimited list" + [Environment]::NewLine)
  145.             }
  146.             $sb.Append([Environment]::NewLine + ("{0,3}: {1}" -f 0,"To cancel")) | Out-Null  
  147.             return $sb.ToString()
  148.         }
  149.          
  150.         function BuildForm
  151.         {
  152.         PARAM  
  153.         (
  154.             [Parameter(Mandatory=$true)]
  155.             $options,
  156.             [string]$displayProperty,
  157.             [string]$title = "Select Item"
  158.         )
  159.             $script:selectedItem = $null
  160.              
  161.             $form.Size = new-object System.Drawing.Size @(235,250)    
  162.             $form.text = $title  
  163.              
  164.             #Create the list box.
  165.              
  166.             $lstOptions.Name = "lstOptions"
  167.             $lstOptions.Width = 210
  168.             $lstOptions.Height = 175
  169.             $lstOptions.Location = New-Object System.Drawing.Size(5,5)
  170.             $lstOptions.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
  171.                  
  172.             #Create the OK button
  173.             [System.Windows.Forms.Button]$btnOK = New-Object System.Windows.Forms.Button  
  174.             $btnOK.Width=100
  175.             $btnOK.Location = New-Object System.Drawing.Size(110, 180)
  176.             $btnOK.Text = "OK"
  177.             $btnOK.add_click({processOK})
  178.             $btnOK.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
  179.             $form.Controls.Add($lstOptions)
  180.             $form.Controls.Add($btnOK)
  181.              
  182.             #Create the Cancel button
  183.             [System.Windows.Forms.Button]$btnCancel = New-Object System.Windows.Forms.Button  
  184.             $btnCancel.Width=100
  185.             $btnCancel.Location = New-Object System.Drawing.Size(5, 180)
  186.             $btnCancel.Text = "Cancel"
  187.             $btnCancel.add_click({processCancel})
  188.             $btnCancel.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
  189.              
  190.             $form.Controls.Add($lstOptions)
  191.             $form.Controls.Add($btnOK)
  192.             $form.Controls.Add($btnCancel)
  193.              
  194.             #Populate ListBox
  195.             foreach ($option in $options)
  196.             {
  197.                 if ([String]::IsNullOrEmpty($displayProperty))
  198.                 {
  199.                     $lstOptions.Items.Add($option) | Out-Null
  200.                 }
  201.                 else
  202.                 {
  203.                     $lstOptions.Items.Add($option.$displayProperty) | Out-Null
  204.                 }
  205.             }
  206.             return $form
  207.         }
  208.          
  209.         switch($mode.ToLower())  
  210.         {
  211.             "menu"
  212.                 {
  213.                     [string]$menuText = BuildMenu -options $options -DisplayProperty $displayProperty -title $title
  214.                     Write-Host $menuText
  215.                     [string]$responseString = Read-Host "Enter Selection"
  216.                     if (-not [String]::IsNullOrEmpty($responseString))
  217.                     {
  218.                         $script:selectedItem = $null
  219.                         [int]$index = 0
  220.                         if ($selectMultiple)
  221.                         {
  222.                             $responses = $responseString.Split(',')
  223.                             foreach ($response in $responses)
  224.                             {
  225.                                 $index = [int]$response
  226.                                 if ($response -gt 0 -and $response -le $options.Count)
  227.                                 {
  228.                                     if ($script:selectedItem -eq $null)
  229.                                     {
  230.                                         $script:selectedItem = @($options[$response-1])
  231.                                     }
  232.                                     else
  233.                                     {
  234.                                         $script:selectedItem += $options[$response-1]
  235.                                     }
  236.                                 }
  237.                             }
  238.                         }
  239.                         else
  240.                         {
  241.                             $index = [int]$responseString
  242.                             $script:selectedItem = @($options[$index-1])
  243.                         }
  244.                          
  245.                     }
  246.                 }
  247.             "checkedbistbox"  
  248.                 {  
  249.                     $fm = BuildForm -options $options -DisplayProperty $displayProperty -title $title
  250.                     $fm.ShowDialog() | Out-Null
  251.                 }
  252.             default  
  253.                 {  
  254.                     $fm = BuildForm -options $options -DisplayProperty $displayProperty -title $title
  255.                     $fm.ShowDialog() | Out-Null
  256.                 }
  257.         }
  258.         Write-Output $script:selectedItem
  259.     }
  260.  
  261.  
  262.  
  263.    
  264.  
  265. $Item = Select-ItemFromList -options "Item One","Item Two","Item Three","Item Four","Item Five" -mode ListBox -selectionMode One
  266.  
  267. write-host You Picked $item -fore green
Advertisement
Add Comment
Please, Sign In to add comment