Public/Read-MultiDataSequential.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<#
.SYNOPSIS
Reads properties for multiple items.

.DESCRIPTION
Read-MultiDataSequential accepts a base object and a list of properties. While values are being entered,
generate a new object from the base object and the additional properties.

To stop entering values, provide no value for any property for a whole object.

.EXAMPLE
PS C:\> Read-MultiData -BaseObject ([pscustomobject]@{ Test = $true }) -Properties InstanceNumber

Prompts for InstanceNumber 3 times, emitting objects with two properties (InstanceNumber with the supplied value, and Test being $true).
#>

function Read-MultiDataSequential {
    [CmdletBinding()]
    PARAM (
        # Property names to populate
        [Parameter(Mandatory)][string[]]$Properties,
        # A base object.
        [psobject]$BaseObject = [pscustomobject]@{}
    )

    Write-Host "To exit, enter no value for one whole object."
    for(;;){
        [array]$Data = foreach($Name in $Properties){
            [pscustomobject]@{
                Key = $Name
                Value = Read-Host -Prompt $Name
            }
        }

        if(!($Data.Value | Where-Object { ![string]::IsNullOrEmpty($_) })){
            break
        }

        Select-Object -InputObject $BaseObject -Property @(
            '*'
            foreach($Datum in $Data){
                @{
                    Name = $Datum.Key
                    Expression = [scriptblock]::Create("""$($Datum.Value -replace '"', '""')""")
                }
            }
        )
    }
}