Public/Watch-P1ServerModel.ps1

function Watch-P1ServerModel {
    <#
    .Synopsis
    Watch the model versions on PlannerOne server.

    .Description
    Display for each environment loaded in PlannerOne the real model and cache versions.

    .Parameter Tenant
    The tenant to use to retrieve connection informations.

    .Parameter HostName
    The hostname to query (default localhost)

    .Parameter ServicePort
    The hostname port of REST Service (default 80)

    .Example
    Watch-P1ServerModel -ServicePort 8073
    #>


    [cmdletbinding()]
    param(
        [string] $Tenant,
        [string] $HostName,
        [int] $ServicePort
    )
    Process
    {
        if ($Tenant -ne "") {
            $info = Get-TenantInfo $Tenant
            if ($info -eq $null) {
                Write-Warning "No information registered for tenant $Tenant"
                return
            }

            $HostName = $info.ServiceHost
            $ServicePort = $info.WebServicePort
        } else {
            $Tenant = "NA"
        }

        if ($HostName -eq "") {
            $HostName = "localhost"
        }

        if ($ServicePort -eq 0) {
            $ServicePort = 80
        }

        $url = 'http://' + $HostName + ':' + $ServicePort + '/Services/cache/all'
        $serviceUrl = FixUrl $url
        $auto = $true
        $continue = $true
        while($continue)
        {
            clear
            echo "Tenant: $Tenant - Host: $HostName - Port: $ServicePort"
            $lastRefresh = Get-Date
            echo "$lastRefresh - Version format: Planning | Static | Dynamic"
            $state = (wget $serviceUrl).Content
            $stateObj = $state | ConvertFrom-Json
            $displayArray = @()
            foreach ($env in $stateObj) {

                if ($env.Id.EnvironmentId.Kind -eq 1) {
                    $kind = "PS"
                } else {
                    $kind = "RP"
                }

                if ($env.Id.Nature -eq 1) {
                    $nature = "Working"
                } else {
                    $nature = "Published"
                }

                $real = Get-PlanningVersion $env.RealPlanningVersion
                $vo = Get-PlanningVersion $env.VOVersion
                $ro = Get-PlanningVersion $env.ReadOnlyVersion


                $displayObj = New-Object System.Object
                $displayObj | Add-Member -type NoteProperty -name Product -value $kind
                $displayObj | Add-Member -type NoteProperty -name Name -value $env.Id.EnvironmentId.Name
                $displayObj | Add-Member -type NoteProperty -name Group -value $env.Id.EnvironmentId.PlanningGroupContainer
                $displayObj | Add-Member -type NoteProperty -name Nature -value $nature
                $displayObj | Add-Member -type NoteProperty -name Real_Version -value $real
                $displayObj | Add-Member -type NoteProperty -name Cache_VOVersion -value $vo
                $displayObj | Add-Member -type NoteProperty -name Cache_ReadOnlyVersion -value $ro

                $displayArray += $displayObj
            }

            $displayArray | Format-Table

            echo "ESCAPE: Quit | R: Refresh | A: Auto-Refresh (5s): $auto"
            $counter = 0
            if ($auto) {
                while(![console]::KeyAvailable -and ($counter++ -lt 10))
                {
                    [Threading.Thread]::Sleep( 500 )
                }
            }

            if ([console]::KeyAvailable -or !$auto)
            {
               $x = [System.Console]::ReadKey()

               switch ($x.key)
               {
                   # http://msdn.microsoft.com/en-us/library/system.consolekey(v=vs.110).aspx
                   Escape { $continue = $false }
                   R { }
                   A { $auto = !$auto }
               }
            }
        }

        Write-Output "Watch end"
        # Write-Host must be call for ReadKey to not corrupt the console
        Write-Host
    }
}