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 Environments
    The PlannerOne environments list to toggle auto publish value.
 
    .Parameter Tenant
    The PlannerOne tenant.
     
    .Parameter WebPort
    The PlannerOne web application port.
 
    .Parameter WebAppName
    The PlannerOne web application name.
 
    .Example
    # Toggle auto publish value on all environments in $env array
    Update-P1ToggleEnvironmentsAutoPublish -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/toggleAutoPublish"
        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)

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

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