MultiChoice.psm1


<#
.SYNOPSIS
    Generate a Multichoice from multi-valued object and return the selected object
.DESCRIPTION
    Generate a Multichoice from multi-valued object and return the selected object (can accept pipeline input)
.NOTES
    Authors : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk
    Version : 1.0.4
    Date : 2022.07.15
        Update : 1.0.4
                    SG - 2022.07.15
                    Removed selection requirement if only 1 InputItem specified
        Update : 1.0.3
                    SG - 2022.05.07
                    Removed additional variable output
        Update : 1.0.2
                    SG - 2022.05.07
                    Renamed InputObject
        Update : 1.0.1
                    SG - 2022.05.02
                    Updated after running Script Analyser
        Update : 1.0.0
                    SG - 2022.05.02
                    Initial Script
.LINK
    http://www.FocusedIT.co.uk
#>

function Invoke-MultiChoice{

    <#
    .SYNOPSIS
        Generate a Multichoice from multi-valued object and return the selected object
    .DESCRIPTION
        Generate a Multichoice (using Write-Host) from multi-valued object and return the selected object (can accept pipeline input)
    .EXAMPLE
        Invoke-MultiChoice -Input $ListOfNames
    .EXAMPLE
        $ListOfComputers | Invoke-MultiChoice -Property ComputerName
    .NOTES
        Authors : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk
        Version : 1.0.4
        Date : 2022.07.15
    .LINK
        http://www.FocusedIT.co.uk
    #>


    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True)]
        [Object[]]$InputObject,
        [Parameter(ValueFromPipeline=$False)]
        $Property
    )
    begin{
        $Items = @()
    }
    process{
        foreach($i in $InputObject){
            $Items += $i
        }
    }
    end{
        $InputCount = $Items.count
        Write-Verbose "Count $InputCount"
        $Max = $InputCount -1
        $i = 0
        if(!($Max -eq 0)){
            while($i -le $Max){
                if($Property){
                    $Current = ($Items[$i]).$Property
                }else{
                    $Current = $Items[$i]
                }
                Write-Host "$i $Current "
                $i ++
            }
            $Choice = Read-Host "Enter Selection Number"
            if($Choice -match "\d{1}"){
                $Result = $Items[$Choice]
            }else{
                $Search = $Items | ?{$_ -match $Choice}
                if($Search.count -eq 1){
                    $Result = $Search
                }else{
                    Write-Warning "Please be more specific.."
                }
            }
        }else{
            $Result = $Items[0]
        }
        return $Result
    }


}