Public/Initialize-P1Environments.ps1

function Initialize-P1Environments {
    <#
    .Synopsis
    Initialize PlannerOne environments
 
    .Description
    Initialize 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
    # Initialize all environments in $env array
    Initialize-P1Environments -Environments $env -WebPort 80 -WebAppName "PlannerOneDev"
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$true)]
        [object] $Environments,
        [int] $WebPort,
        [string] $Tenant,
        [string] $WebAppName
    )
    Process
    {
    
    $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/init"

    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" }
            $encapsulate = $Environments[$i].EnvironmentId | ConvertTo-Json -Compress
            $body = @{
                environmentId = @{JsonValue = $encapsulate};
                product = $Product;
            }

            Write-Host ("Initializing " + $Environments[$i].EnvironmentId + "...")
                Invoke-RestMethod -Method POST -Body ($body | ConvertTo-Json -Compress) -Uri $url -ContentType "application/json" -UseDefaultCredentials 

            $progres = 0

            Do
            {
                Start-Sleep -Milliseconds 1000

                $urlProgress = "http://localhost:$WebPort/$WebAppName/configure/ConfigurationService.svc/progress/" + $Product
                $progres = Invoke-RestMethod -Method GET -Uri $urlProgress -ContentType "application/json" -UseDefaultCredentials
                Write-Progress -activity "Initializing in Progress" -status "$progres% Complete:" -percentcomplete $progres
            } While ($progres -le 99)
        }
    }
    }
}