Sample/OSDeployment/OS_Deployment_MultipleTargets_MultipleJobs.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2019 - 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.
# ------------------------------------------------------------------

<#
OS_Deployment_MultipleTargets.ps1
- Example scripts to illustrate how to deploy os image to target systems using a DeployTask array.
#>


# Function to wait for jobs to be deployed to LXCA
function WaitForDeployJobsToStart ([System.Management.Automation.Job[]]$allJobs)
{
    $allJobsStarted = 0
    $sleepTime = 5
    $jobsCount = $allJobs.Count

    Write-Host "Trying to start $jobsCount jobs."

    while($allJobsStarted -eq 0)
    {
        $allJobsStarted = 1
        foreach ($i in $allJobs)
        {
            if ($i.State -eq "NotStarted" )
            {
                $allJobsStarted = 0
                break
            }
        }
        if ($allJobsStarted -eq 0)
        {
            Write-Host "wait for $sleepTime seconds"
            Start-Sleep -Seconds $sleepTime
        }
    }
    # show final status: give some time to update jobs information from LXCA: maybe you have failed tasks.
    Write-Host "Sleeping for 10 seconds..."
    $counter = 10
    while ($counter -gt 0)
    {
        Write-Host -NoNewline "."
        Start-Sleep -Seconds 1
        $counter--
    }
    Write-Host
    Write-Host "Final result:"
    foreach ($i in $allJobs)
    {
        Write-Host "Job $($i.Id) with state $($i.State)"
    }
}

# Define the variable value
$LxcaUserName = "USERID"
$LxcaPassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$LxcaIP = "10.240.197.26"

$UUIDS = @("AAAABBBBCCCCDDDDEEEEFFFF11112222", "BBBBAAAACCCCDDDDEEEEFFFF11112222", "BBBBCCCCAAAADDDDEEEEFFFF11112222", "BBBBCCCCDDDDAAAAEEEEFFFF11112222")

# First connect to LXCA server
$Cred = New-Object System.Management.Automation.PSCredential($LxcaUserName, $LxcaPassword)
Connect-LXCA $LxcaIP -Credential $Cred -SkipCertificateCheck


$jobs = @()

$UUIDS | foreach {
    $deployTask = New-LXCADeployTask -ServerUuid $_ -ImageProfileID $ImageProfileID -TargetDevice $TargetDevice -MacAddress "AUTO"

    $ret = Install-LXCAOSImage -DeployTask $deployTask -AsJob
    if ($ret -eq $null)
    {
        Write-Host "Install-LXCAOSImage is failed with unexcepted error!" -ForegroundColor Red
    }
    else
    {
        Write-Host "Deploy kicked off to $_ => job id $($ret.Id) with state $($ret.State)"
        $jobs += $ret
    }
}

# Wait for job to be deployed and Started on LXCA. There are two possible outputs:
# Job started successfuly on LXCA:
# Job 16 with state Running
# Job Failed to start on LXCA:
# Job 8 with state Failed

WaitForDeployJobsToStart($jobs)

# Disconnect from LXCA server
Disconnect-LXCA