Functions/LabEnvironment/Register-LabEnvironment.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 60 61 62 63 64 65 |
#Requires -Version 5.0 function Register-LabEnvironment { [CmdletBinding(DefaultParameterSetName = 'EnvironmentPath', SupportsShouldProcess = $true, ConfirmImpact = 'High')] param ( [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'EnvironmentPath')] [string]$Path, [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'EnvironmentObject', ValueFromPipeline = $true)] [LabEnvironment[]]$Environment, [Parameter(Mandatory = $false)] [switch]$PassThru, [Parameter(Mandatory = $false)] [switch]$Force ) Process { if ($($PSCmdlet.ParameterSetName) -eq 'EnvironmentPath') { if (-not $Path) { throw 'Provide a path to the environment.' } if (Test-Path -Path $Path -PathType Container) { Write-Verbose "path '$Path' is folder, assuming filename is missing" $Path = Join-Path -Path $Path -ChildPath 'environment.json' } if (-not (Test-Path -Path $Path -PathType Leaf)) { throw 'The provided path does not reference an existing environment-file.' } $Environment = Get-LabEnvironment -Path $Path } if (-not $Environment) { return } $filePath = Join-Path -Path $script:configurationPath -ChildPath 'environments.json' if (Test-Path -Path $filePath -PathType Leaf) { $environments = Get-Content -Path $filePath -Raw | ConvertFrom-Json | Convert-PSObjectToHashtable } if (-not $environments) { $environments = @{} } $update = $false foreach ($e in $Environment) { if (-not $environments.ContainsKey($e.Name) -or $Force.IsPresent -or $PSCmdlet.ShouldProcess($e.Name)) { $environments.($e.Name) = $e.Path $update = $true } } if ($update) { if (-not (Test-Path -Path (Split-Path -Path $filePath -Parent) -PathType Container)) { New-Item -Path (Split-Path -Path $filePath -Parent) -ItemType Directory -Force | Out-Null } $environments | ConvertTo-Json -Depth 9 | Out-File -FilePath $filePath } if ($PassThru.IsPresent) { return $Environment } } } |