Functions/LabEnvironment/Unregister-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 |
#Requires -Version 5.0 function Unregister-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 (-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 ($environments) { $update = $false foreach ($e in $Environment) { if ($environments.ContainsKey($e.Name) -and ($Force.IsPresent -or $PSCmdlet.ShouldProcess($e.Name))) { $environments.Remove($e.Name) $update = $true } } if ($update) { $environments | ConvertTo-Json -Depth 9 | Out-File -FilePath $filePath } } } if ($PassThru.IsPresent) { return $Environment } } } |