Sample/OSDeployment/OS_Deployment_MultipleTargets_SingleJob.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
$LxcaIP = "192.0.2.0"

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

# First connect to LXCA server
$Cred = Get-Credential -Message "Credential to access LXCA"
Connect-LXCA $LxcaIP -Credential $Cred -SkipCertificateCheck

# create an array for DeployTasks objects.
$deployTasks = @()

# populate the array for each uuid
$UUIDS | ForEach-Object
{
    $deployTasks += New-LXCADeployTask -ServerUuid $_ -ImageProfileID $ImageProfileID -TargetDevice $TargetDevice -MacAddress "AUTO"
}

# Send a single deploy OS command to multiple targets.
$ret = Install-LXCAOSImage -DeployTask $deployTasks -AsJob

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

# 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

$jobs = @($ret)
WaitForDeployJobsToStart($jobs)

# Disconnect from LXCA server
Disconnect-LXCA