Public/Watch-P1WebModel.ps1

function Watch-P1WebModel {
    <#
    .Synopsis
    Watch the model versions on PlannerOne web.
 
    .Description
    Display for each session in PlannerOne Web application the model 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 PlannerOne web Service (default 80)
 
    .Parameter WebAppName
    The web application name of PlannerOne (default PlannerOne)
 
    .Example
    Watch-P1WebModel -ServicePort 8073
    #>


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

            $HostName = $info.WebHost
            $ServicePort = $info.SitePort
            $WebAppName = $info.WebApplicationName
        } else {
            $Tenant = "NA"
        }

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

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

        if ($WebAppName -eq "") {
            $WebAppName = "PlannerOne"
        }

        $url = 'http://' + $HostName + ':' + $ServicePort + '/' + $WebAppName + '/Api/CacheInfo.svc/versions'
        $serviceUrl = FixUrl $url
        $auto = $true
        $clean = $true
        $continue = $true
        while($continue)
        {
            clear
            echo "Tenant: $Tenant - Host: $HostName - Port: $ServicePort - WebApp: $WebAppName"
            $lastRefresh = Get-Date
            echo "$lastRefresh - Version format: Planning | Static | Dynamic"
            $stateJson = (wget $serviceUrl -UseDefaultCredentials).Content
            $state = $stateJson | ConvertFrom-Json
            Write-Verbose "state: $state"
            if ($state -ne $null) {
                $web = "WebApplication: " + $state.Name
                 # echo $web
                foreach ($session in $state.Sessions) {
                    if ($clean -eq $false -or $session.Environments -ne $null) {
                        $sess = "Session: " + $session.Id
                        echo $sess
                        foreach ($env in $session.Environments) {
                            $environment = "|_Environment: " + $env.Key
                            echo $environment
                            $vw = Get-PlanningVersionWeb $env.Working
                            echo "| |_Working:`t$vw"
                            $vp = Get-PlanningVersionWeb $env.Published
                            echo "| |_Published:`t$vp"
                            foreach ($tab in $env.Tabs) {
                                $t = $tab.Key
                                $v = "View: " + $tab.Version.View + " | Last: " + $tab.Version.Last
                                echo "| |_$t : $v"
                            }

                            echo "|"
                        }

                        echo ""
                    }
                }
            }

            echo "ESCAPE: Quit | R: Refresh | A: Auto-Refresh (5s): $auto | C: Clean empty sessions: $clean"
            $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 }
                   C { $clean = !$clean }
               }
            }
        }

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