-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathGet-ALZConfig.ps1
More file actions
63 lines (56 loc) · 2.28 KB
/
Get-ALZConfig.ps1
File metadata and controls
63 lines (56 loc) · 2.28 KB
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
60
61
62
63
function Get-ALZConfig {
param(
[Parameter(Mandatory = $false)]
[string] $configFilePath = "",
[Parameter(Mandatory = $false)]
[PSCustomObject] $inputConfig = $null,
[Parameter(Mandatory = $false)]
[string] $hclParserToolPath = ""
)
if (!(Test-Path $configFilePath)) {
Write-Error "The config file does not exist at $configFilePath"
throw "The config file does not exist at $configFilePath"
}
if ($null -eq $inputConfig) {
$inputConfig = [PSCustomObject]@{}
}
# Import the config and transform it to a PowerShell object
$extension = (Get-Item -Path $configFilePath).Extension.ToLower()
$config = $null
if ($extension -eq ".yml" -or $extension -eq ".yaml") {
try {
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Yaml -Ordered)
} catch {
$errorMessage = "Failed to parse YAML inputs. Please check the YAML file for errors and try again. $_"
Write-Error $errorMessage
throw $errorMessage
}
} elseif ($extension -eq ".json") {
try {
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Json)
} catch {
$errorMessage = "Failed to parse JSON inputs. Please check the JSON file for errors and try again. $_"
Write-Error $errorMessage
throw $errorMessage
}
} elseif ($extension -eq ".tfvars") {
try {
$config = [PSCustomObject](& $hclParserToolPath $configFilePath | ConvertFrom-Json)
} catch {
$errorMessage = "Failed to parse HCL inputs. Please check the HCL file for errors and try again. $_"
Write-Error $errorMessage
throw $errorMessage
}
} else {
throw "The config file must be a json, yaml/yml or tfvars file"
}
Write-Verbose "Config file loaded from $configFilePath with $($config.PSObject.Properties.Name.Count) properties."
foreach ($property in $config.PSObject.Properties) {
$inputConfig | Add-Member -NotePropertyName $property.Name -NotePropertyValue @{
Value = $property.Value
Source = $extension
Sensitive = $false
}
}
return $inputConfig
}