Public/Read-MultiData.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
50
51
52
53
54
55
56
57
58
<#
.SYNOPSIS
Reads properties for multiple objects in parallel, then outputs values for all.

.DESCRIPTION
Reads properties for multiple objects in parallel, then outputs new objects for all.

The properties parameter is a map of key names to constants (passed through), script blocks (evaluated with the index), and $null (queries for a value).
#>

function Read-MultiData {
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory)]
            # The number of objects to get data for.
            [ValidateScript({ $_ -gt 0 })]
            [int]$Count,
        [ValidateNotNullOrEmpty()]
            # Property mappings to populate. Scriptblock values will be passed the current index, constant values will pass through, and null values will prompt a query.
            [hashtable]$Properties = @{},
        [ValidateNotNullOrEmpty()]
            # Extra properties to query for.
            [string[]]$QueryProperty = @()
    )

    $ObjectProperties = @(
        $Properties.GetEnumerator().ForEach({ $_ })
        $QueryProperty | Where-Object { $_ } | ForEach-Object {
            [pscustomobject]@{
                Key = $_
                Value = $null
            }
        }
    )

    [array]$ValueData = foreach($Property in $ObjectProperties){
        [pscustomobject]@{
            Name = $Property.Key.ToString()
            Values = [array]@(foreach($Index in 0..($Count - 1)){
                if($null -eq $Property.Value){
                    Read-Host -Prompt $Property.Key
                } elseif($Property.Value -is [scriptblock]) {
                    $Index | ForEach-Object -Process $Property.Value
                } else {
                    $Property.Value
                }
            })
        }
    }

    foreach($Index in 0..($Count - 1)){
        $Values = [ordered]@{}
        foreach($Entry in $ValueData){
            $Values[$Entry.Name] = $Entry.Values[$Index]
        }
        [pscustomobject]$Values
    }
}