CosmosJobMonitor.psm1

function notify {
    Param (
        [Parameter(Mandatory=$true)]
        [string]
        $Title,
        [Parameter(Mandatory=$true)]
        [string]
        $Text
    )

    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
    $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)

    $netXml = [xml] $template.GetXml()
    $netXml.SelectNodes("//text[@id='1']").AppendChild($netXml.CreateTextNode($Title)) > $null
    $netXml.SelectNodes("//text[@id='2']").AppendChild($netXml.CreateTextNode($Text)) > $null

    $winXml = New-Object Windows.Data.Xml.Dom.XmlDocument
    $winXml.LoadXml($netXml.OuterXml)

    $Toast = [Windows.UI.Notifications.ToastNotification]::new($winXml)
    $Toast.Tag = "PowerShell"
    $Toast.Group = "PowerShell"
    $Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)

    $Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
    $Notifier.Show($Toast);
}

function getMessage {
    Param (
        [string]
        $CurrentQueued,
        [string]
        $CurrentRuning,
        [string]
        $Path,
        [string]
        $User
    )

    return "Found $currentQueued job$(if($currentQueued -ne 1) {'s'}) queued and $currentRunning job$(if($currentRunning -ne 1) {'s'}) running";
}

function Watch-CosmosJobs
{
    Param (
        [Parameter(Mandatory=$true)]
        [string]
        $Path,
        [Parameter(Mandatory=$true)]
        [string]
        $User
    )

    $ProgressTitle = "Monitoring COSMOS jobs for $User on $Path"
    $PollingInterval = 30;

    Write-Progress -Activity $ProgressTitle -CurrentOperation "Starting"

    $watchedRunning = 0;
    $watchedQueued = 0;

    try {
        $currentRunning = (Get-CosmosJob -path $Path -user $User -State Running | Measure-Object).Count
    }
    catch [Exception]
    {
        Connect-AzAccount
        $currentRunning = (Get-CosmosJob -path $Path -user $User -State Running | Measure-Object).Count
    }

    $currentQueued = (Get-CosmosJob -path $Path -user $User -State Queued | Measure-Object).Count

    $Message = getMessage -CurrentQueued $currentQueued -CurrentRuning $currentRunning -Path $Path -User $User
    notify -Title "COSMOS job monitor started" -Text $Message

    do
    {
        $Message = getMessage -CurrentQueued $currentQueued -CurrentRuning $currentRunning -Path $Path -User $User
    
        if ($currentQueued -gt $watchedQueued)
        {
            notify -Title "COSMOS job queued" -Text $Message
        } 
        elseif ($currentRunning -gt $watchedRunning) 
        {
            notify -Title "COSMOS job started" -Text $Message
        } 
        elseif ($currentRunning -lt $watchedRunning) 
        {
            notify -Title "COSMOS job complete" -Text $Message
        }

        for ($sec = $PollingInterval; $sec -gt 0; $sec--)
        {
            Start-Sleep -Seconds 1
            Write-Progress -Activity $ProgressTitle -CurrentOperation "Waiting $($PollingInterval)s until next poll" -Status $Message -SecondsRemaining $sec
        }

        $watchedRunning = $currentRunning;
        $watchedQueued = $currentQueued;

        Write-Progress -Activity $ProgressTitle -CurrentOperation "Querying COSMOS for updates" -Status $Message
        $currentRunning = (Get-CosmosJob -path $Path -user $User -State Running -ErrorAction Stop | Measure-Object).Count
        $currentQueued = (Get-CosmosJob -path $Path -user $User -State Queued -ErrorAction Stop | Measure-Object).Count
    } while(($currentRunning + $currentQueued) -gt 0);

    Write-Progress -Activity $ProgressTitle -Completed
    notify -Title "COSMOS job monitor stopped" -Text "No outstanding jobs found"
}

Export-ModuleMember -Function Watch-CosmosJobs