Public/Update-P1ToggleEnvironmentsAutoPublish.ps1

function Update-P1ToggleEnvironmentsAutoPublish {
    <#
    .Synopsis
    Toggle auto publish on PlannerOne environments

    .Description
    Toggle auto publish on PlannerOne environments. The environment list must be up to date.
    This command works only with PlannerOne project environments.

    .Parameter Tenant
    The PlannerOne tenant.

    .Parameter Environments
    The PlannerOne environments list to toggle auto publish value.

    .Example
    # Toggle auto publish value on all environments in $env array
    Update-P1ToggleEnvironmentsAutoPublish -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 ("Toogle auto publish for environment:")
                Write-Section (" Compagny name: " + $Environments[$i].EnvironmentId.Name)
                Write-Section (" PGC: " + $Environments[$i].EnvironmentId.PlanningGroupContainer)

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

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