Public/New-CsvFile.ps1

<#
    .SYNOPSIS
    Create a new template scenario CSV file to contain measurement data.
 
    .DESCRIPTION
    Used to create a new template metric CSV file. The command will prompt with metric definitions and possible units
    from the org. Select one or more rows and the file will be created those in template form.
 
    The file created is used by the Add-PatientScenario cmdlet.
 
    .INPUTS
    None. You cannot pipe objects to New-CsvFile.
 
    .OUTPUTS
    The new configuration
 
    .PARAMETER Filename
    The file name to create. Defaults to "data.csv"
 
    .PARAMETER MetricDefinitions
    The metric definitions from Get-MetricDefinitions. Calls Get-MetricDefinitions is not supplied.
 
    .EXAMPLE
    PS> New-CsvFile
 
    .LINK
    Set-Fileconfig
    Get-MetricDefinitions
    Add-PatientScenario
 
    .NOTES
    Assumes config is initialized for org access.
#>

function New-CsvFile {
    param($Filename = "data.csv", $MetricDefinitions)

    if (-not $PSBoundParameters.ContainsKey('MetricDefinitions')) {
        $MetricDefinitions = Get-MetricDefinitions
    }
    $options = @()
    foreach ($measurement in $MetricDefinitions.measurements) {
        $measurementMetrics = $MetricDefinitions.measurementMetrics | Where-Object { $_.phecc__Measurement_Type__c -eq $measurement.id }
        foreach ($measurementMetric in $measurementMetrics) {
            $metrics = $MetricDefinitions.metrics | Where-Object { $_.Id -eq $measurementMetric.phecc__Metric__c }
            foreach ($metric in $metrics) {
                #$column1 = "$($metric.Name)"
                $metricUnits = $MetricDefinitions.metricUnits | Where-Object { $_.phecc__Metric__c -eq $metric.Id }
                foreach ($metricUnit in $metricUnits) {
                    #$column2 = $Row."$($metric.Name):units"
                    $units = $MetricDefinitions.units | Where-Object { $_.Id -eq $metricUnit.phecc__Unit__c}
                    foreach ($unit in $units) {
                        $options += @{
                            measurement = $measurement
                            metric = $metric
                            unit = $unit
                        }
                    }
                }
            }
        }
    }
    $selected = $options | Select-Object @{Name='Measurement';expression={($_.measurement.Name)}},@{Name='Metric';expression={($_.metric.Name)}},@{Name='Unit';expression={($_.unit.Name)}},@{Name='Object';expression={($_)}}  | Out-GridView -PassThru
    $Columns = @("MinuteOffset","Type","Name")
    $selected | ForEach-Object {
        $Columns += "$($_.Object.metric.Name)"
        $Columns += "$($_.Object.metric.Name):units"
    }
    $Rows = @()
    $selected | ForEach-Object {
        $nextSelected = $_
        $Row = New-Object PSObject
        $Columns | ForEach-Object {
            $value = ""
            if ($selected.Object.metric.Name) {
                switch ($_) {
                    "MinuteOffset" { $value = 0 }
                    "Type" { $value = "Measurement" }
                    "Name" { $value = "$($nextSelected.Object.measurement.Name)" }
                    "$($nextSelected.Object.metric.Name)" { $value = "0" }
                    "$($nextSelected.Object.metric.Name):units" { $value = $nextSelected.Object.unit.Name }
                }
                $Row | Add-Member -NotePropertyName $_ -NotePropertyValue $value
            }
        }
        $Rows += $Row
    }
    $Rows | ConvertTo-Csv | Set-Content -Path $Filename
}