[SOLVED] Common section in Powershell

Discussion in 'Scripting' started by sebus, Jun 22, 2017.

  1. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,770
    1,727
    180
    #1 sebus, Jun 22, 2017
    Last edited: Jul 30, 2017
    I have PS scrip that presents two item menu.

    I have common section that should apply to either selection.
    But I can not get it working. The only way it to add all from that section to each of the two function sections (but that makes script bigger for no reason)

    Code:
    # define a task list with the messages to display and the functions to invoke:
    $taskList = @(
        [PSCustomObject]@{Message = 'File Selection'; Task = { FileSelect }}
        [PSCustomObject]@{Message = 'Folder Selection'; Task = { FolderSelect }}
      )
    
    # define the functions:
    
    function FolderSelect()
    {
        Write-Host "Folder Selection"
     
    function FileSelect()
    {
        Write-Host "File Selection"
    
    # let the user pick a task:
    Write-Host -ForegroundColor Yellow "Choose a task:"
    
    $taskList | foreach -Begin { $i = 1;} -Process {
        Write-Host -ForegroundColor Yellow ('{0}. {1}' -f ($i++), $_.Message)
    }
    
    do
    {
        $value = Read-Host 'Choose a number from the task list'
    
    }
    while($value -match '\D+' -or $value -le 0 -or $value -gt $taskList.Count)
    
    # invoke the task:
    & $taskList[$value-1].Task
    
    
    Need a common section to be run after either selection
    
    Help appreciated for pointing what I am missing
     
  2. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,770
    1,727
    180
    #2 sebus, Jun 27, 2017
    Last edited: Jul 30, 2017
    (OP)
    Define "Common" section as

    function ProcessSelection()

    and just called it from within each other existing function

    So batch :COMMON is really PS function