Public/Remove-P1Environments.ps1

function Remove-P1Environments {
    <#
    .Synopsis
    Remove PlannerOne environments
 
    .Description
    Remove PlannerOne environments
 
    .Parameter Environments
    The PlannerOne environments list to initialized.
 
    .Parameter Tenant
    The PlannerOne tenant.
     
    .Parameter WebPort
    The PlannerOne web application port.
 
    .Parameter WebAppName
    The PlannerOne web application name.
 
    .Example
    # Remove all environments in $env array
    Remove-P1Environments -Environments $env -WebPort 80 -WebAppName "PlannerOneDev"
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$true)]
        [object] $Environments,
        [int] $WebPort,
        [string] $Tenant,
        [string] $WebAppName
    )
    Process
    {    
        if (!(Test-Tenant $Tenant)) {
            Write-Warning "Tenant $Tenant does not exist."
            Write-Warning "Operation canceled."
            return;
        }

        $TenantInfo = Get-P1Tenant $Tenant
        
        if ($WebPort -eq 0) {
            $WebPort = $TenantInfo.SitePort
        }
            
        if (!$WebAppName) {
            $WebAppName = $TenantInfo.WebApplicationName
        }

        $url = "http://localhost:$WebPort/$WebAppName/configure/ConfigurationService.svc/environments"
        for($i=0; ($i -le $Environments.Length); $i++)
        {
            if ($Environments[$i].EnabledInErp -and $Environments[$i].IsInPlannerOne) 
            {
                If ($Environments[$i].EnvironmentId.Kind -eq 2) { $Product = "Project" } 
                ElseIf ($Environments[$i].EnvironmentId.Kind -eq 1) { $Product = "Production" }
                
                Write-Section ("Delete environments:")
                Write-Section (" Compagny name: " + $Environments[$i].EnvironmentId.Name)
                Write-Section (" PGC: " + $Environments[$i].EnvironmentId.PlanningGroupContainer)

                $encapsulate = $Environments[$i] | ConvertTo-Json -Compress
                $body = @{
                    environment = @{JsonValue = $encapsulate};
                    product = $Product;
                }

                Invoke-RestMethod -Method DELETE -Uri $url -Body ($body | ConvertTo-Json -Compress) -ContentType "application/json" -UseDefaultCredentials
            }
        }
    }
}