Public/Remove-P1Environments.ps1

function Remove-P1Environments {
    <#
    .Synopsis
    Remove PlannerOne environments

    .Description
    Remove PlannerOne environments

    .Parameter Tenant
    The PlannerOne tenant.

    .Parameter Environments
    The PlannerOne environments list to initialized.

    .Example
    # Remove all environments in $env array
    Remove-P1Environments -Tenant PROD -Environments $env
    #>

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

        $info = Get-P1Tenant $Tenant

        $hostName = $info.ServiceHost
        $servicePort = $info.WebServicePort

        $envNumber = $Environments.Length
        Write-Verbose "Number of environments: $envNumber"

        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 environment:")
                Write-Section (" Compagny name: " + $Environments[$i].EnvironmentId.Name)
                Write-Section (" PGC: " + $Environments[$i].EnvironmentId.PlanningGroupContainer)

                $url = 'http://' + $hostName + ':' + $servicePort + "/Services/configuration$Product/environments"
                Write-Verbose "url: $url"
                $serviceUrl = FixUrl $url

                $postBody = $Environments[$i] | ConvertTo-Json
                Invoke-WebRequest -Uri $serviceUrl -Method DELETE -Body $postBody -UseBasicParsing -ContentType "application/json;charset=utf-8" | Out-Null
            }
        }
    }
}