Samples/E2E-hub-devices/common/common.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2026 - present.
#
# LIMITED AND RESTRICTED RIGHTS NOTICE:
# If data or software is delivered pursuant a General Services
# Administration (GSA) contract, use, reproduction, or disclosure
# is subject to restrictions set forth in Contract No. GS-35F-05925.
# ------------------------------------------------------------------

function PrintProgerss()
{
    param (
        [Parameter(Mandatory = $true)]
        [int] $Count
    )
    if ($Count -le 20)
    {
        Write-Host "." -NoNewline
        $Count ++
    }
    else
    {
        Write-Host "."
        $Count = 0
    }
    return $Count
}

function GetJobList()
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [System.Object] $Jobs
    )

    [int[]]$jobList = @()
    if (($Jobs -is [System.String]) -and ($jobID = [int]$Jobs))
    {
        $jobList += $jobID
    }
    elseif ($Jobs -is [System.Collections.IEnumerable])
    {
        foreach ($item in $Jobs)
        {
            if ($jobID = [int]$item)
            {
                $jobList += $jobID
            }
        }
    }
    else
    {
        Write-Host $Jobs | Format-List
        throw "Invalid input for Jobs parameter. Expected a single job ID or a collection of job IDs."
    }
    return $jobList
}

function IsJobCompleted()
{
    param (
        [Parameter(Mandatory = $true)]
        [System.String] $Job
    )
    return (-not $Job.ToLower().Contains("running"))
}

function WaitForTasksToEnd ()
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [System.Object] $JobsToWaitFor
    )
    $jobList = GetJobList -Jobs $JobsToWaitFor
    [bool]$isCompleted = $true
    $counter = 0
    $sleepSeconds = 2

    do {
        $isCompleted = $true
        foreach ($jobID in $jobList)
        {
            $job = Get-LXC1Job -Id $jobID
            if (-not (IsJobCompleted -Job $job.Response.status))
            {
                $isCompleted = $false
                break
            }
        }
        if (-not $isCompleted)
        {
            Write-Host ("Elapsed {0} secs ... Sleeping for another {1} seconds." -f ($counter * $sleepSeconds), $sleepSeconds)
            Start-Sleep -Seconds $sleepSeconds
            $counter++
        }
    } while (-not $isCompleted)
}

function ConnectLXC1 ()
{
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $PortalAddress,

        [Parameter(Mandatory = $true)]
        [ValidateSet("onPremises", "cloud")]
        $PortalType,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential]$PortalCredential
    )
    return (Connect-LXC1 -PortalAddress $PortalAddress `
                         -Credential $PortalCredential `
                         -PortalType $PortalType `
                         -Timeout 36000 )
}

#EOF