private/Update-PropertiesFromCsvFile.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
59
function Update-PropertiesFromCsvFile {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [Adf] $adf,
        [Parameter(Mandatory)] [string] $stage
        )

    Write-Debug "BEGIN: Update-PropertiesFromCsvFile(adf=$adf, stage=$stage)"

    $srcFolder = $adf.Location
    if ([string]::IsNullOrEmpty($srcFolder)) {
        Write-Error "adf.Location property has not been provided."
    }
    
    if ($stage.EndsWith(".csv")) { 
        $configFileName = $stage 
    } else {
        $configFileName = Join-Path $srcFolder "deployment\config-$stage.csv"
    }

    Write-Verbose "Replacing values for ADF properties from CSV config file"
    Write-Host "Config file: $configFileName"
    Write-Debug "Testing config file..."
    Test-Path -Path $configFileName -PathType Leaf | Out-Null 

    $configtxt = Get-Content $configFileName | Out-String
    $configcsv = ConvertFrom-Csv $configtxt 
    $cnt = 0

    $configcsv | ForEach-Object {
        Write-Debug "Item: $_"
        $path = $_.path
        $value = $_.value
        $name = $_.name
        $type = $_.type
        $o = Get-AdfObjectByName -adf $adf -name $name -type $type
        if ($null -eq $o) {
            Write-Error "Could not find object: $type.$name"
        }
        $json = $o.Body
        
        Invoke-Expression "`$fieldType = `$json.properties.$path.GetType()"
        Write-Debug "Type of field [$path] = $fieldType"
        if ($fieldType -eq [String]) {
            $exp = "`$json.properties.$path = `"$value`""
        } else {
            $exp = "`$json.properties.$path = $value"
        }
        Invoke-Expression "$exp"

        # Save new file for deployment purposes and change pointer in object instance
        $f = (Save-AdfObjectAsFile -obj $o)
        $o.FileName = $f

        $cnt++
    }
    Write-Host "*** Replaced $cnt properties. ***`n"

}