Sample/OSDeployment/OS_Deployment_custoImage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<#
OS_Deployment_custoImage.ps1
- Example scripts to illustrate how to deploy a custom os image to target systems.
 
Lenovo Copyright
© Copyright Lenovo 2015. 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 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"

$targetSystem = @{
        uuid = "89B8E140DF7C11D49AB09F8B8B8B8B8B"
        systemType = "RackServer"
    }
$deploySet = @{
        macAddress = "40:F2:E9:B8:1B:68"
        imageProfileId = "esxi6.7_1.28|20181205102133_Custom-Unattend"
        targetDevice = "localdisk"        # localdisk / usbdisk / sandisk (SAN ID is required)
        IpAssignment = "dhcpv4"            # dhcpv4 / staticv4 / staticv6
    }

$unattendedFileID = "20181205102110_ESXi_customUnattend.cfg"

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


# Deploy OS image to target system
# Set global deploy setting
$globalSet = Get-LXCADeployGlobalSetting
$globalSet.IpAssignment = $deploySet.IpAssignment
Set-LXCADeployGlobalSetting -DeployGlobalSetting $globalSet

# Create a deploy task
$deployTask = New-LXCADeployTask -MacAddress $deploySet.macAddress -ServerUuid $targetSystem.uuid -ImageProfileID $deploySet.imageProfileId -UnattendedFileID $unattendedFileID -TargetDevice $deploySet.targetDevice -Mtu 1500 -PrefixLength 0

# Deploy OS image to target system
$ret = Install-LXCAOSImage -DeployTask $deployTask
if ($ret -eq $null)
{
    Write-Host "Install-LXCAOSImage is failed with unexcepted error!" -ForegroundColor Red
}
else
{
    Write-Host "Deploy kicked off to $($targetSystem.uuid) => 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