Sample/BulkImport_And_Configuration.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<#
BulkImport_And_Configuration.ps1
- Example scripts to illustrate how to bulk import multiple systems and configuration.
 
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.
#>


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

# Define the configuration pattern (name) for flex compute node and rack server
$configPattern = @{
    flex = "Flex_Pattern"
    rack = "Rack_Pattern"
    }

# Define the bulk import file and username, password.
$bulkImportCsv = "c:\bulkimport.csv"
$serverUserName = "USERID"
$serverPassword = ConvertTo-SecureString "Password" -AsPlainText –Force
$chassisUserName = "USERID"
$chassisPassword = ConvertTo-SecureString "Password" -AsPlainText –Force
$recoveryPassword = ConvertTo-SecureString "Password" -AsPlainText –Force


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

# Bulk import servers
Write-Host "`nTry to manage servers via file:" $bulkImportCsv
$ret = Add-LXCAManagedDevice -BulkImportFile $bulkImportCsv -CurrentRackServerUserName $serverUserName -CurrentRackServerPassword $serverPassword -CurrentChassisUserName $chassisUserName -CurrentChassisPassword $chassisPassword -ChassisRecoveryPassword $recoveryPassword
Write-Host "`nManaged chassis:"
$ret.ChassisManageResult | Format-Table -Wrap
Write-Host "`nManaged rack server:"
$ret.RackServerManageResult | Format-Table -Wrap

# wait until the inventory state is not "Pending" after management
$bWait = $true
while ($bWait)
{
    Start-Sleep -Seconds 20
    Foreach($chassisRet in $ret.ChassisManageResult)
    {
        $allNonPending = $true
        if ($chassisRet.Success -eq $true)
        {
            $chassis = Get-LXCAChassis -ChassisUuid $chassisRet.Uuid
            if ($chassis.AccessState -eq "Pending")
            {
                $allNonPending = $allNonPending -and $false
                break
            }
            else
            {
                $allNonPending = $allNonPending -and $true
            }
            $allNonPending
            $bWait = -not $allNonPending
        }
    }
}

# Configure them via configuration pattern
# Configure compute node
Foreach($chassisRet in $ret.ChassisManageResult)
{
    # Check if the chassis is managed succesfully
    if ($chassisRet.Success -eq $true)
    {
        # Get the compute node list in this chassis
        $computeNodes = Get-LXCAComputeNode -ChassisUuid $chassisRet.Uuid | Select-Object -Property Uuid
        $nodeUuids = @()
        foreach ($node in $computeNodes)
        {
            # config pattern only accepts uuid in upper case
            $nodeUuids += $node.Uuid.ToUpper()
        }
        # Get pattern due to the pattern name
        $pattern = Get-LXCAConfigPattern | where { $_.Name -eq $configPattern.flex}
        if ($pattern -eq $null)
        {
            Write-Host ("`nCannot get pattern with name {0}" -f $configPattern.flex) -ForegroundColor Red
        }
        else
        {
            # Deploy pattern to the compute nodes
            Write-Host "`nDeploy pattern to compute nodes under chassis" $chassisRet.Uuid
            $ret = Install-LXCAConfigPattern -PatternId $pattern.Id -TargetComputeNodeId $nodeUuids -Restart Immediate
            $ret | Format-Table -Property Uuid,Success
        }
    }
}

# wait until the inventory state is not "Pending" after management
$bWait = $true
while ($bWait)
{
    Start-Sleep -Seconds 20
    Foreach($serverRet in $ret.RackServerManageResult)
    {
        $allNonPending = $true
        if ($serverRet.Success -eq $true)
        {
            $server = Get-LXCARackServer -Uuid $serverRet.Uuid
            if ($server.AccessState -eq "PENDING")
            {
                $allNonPending = $allNonPending -and $false
                break
            }
            else
            {
                $allNonPending = $allNonPending -and $true
            }
            $allNonPending
            $bWait = -not $allNonPending
        }
    }
}

# Configure rack server
$serverUuids = @()
Foreach($serverRet in $ret.RackServerManageResult)
{
    # Check if the server is managed succesfully
    if ($serverRet.Success -eq $true)
    {
        # config pattern only accepts uuid in upper case
        $serverUuids += $serverRet.Uuid.ToUpper()
    }
}
if ($serverUuids.Count -gt 0)
{
        # Get pattern due to the pattern name
        $pattern = Get-LXCAConfigPattern | where { $_.Name -eq $configPattern.rack}
        if ($pattern -eq $null)
        {
            Write-Host ("`nCannot get pattern with name {0}" -f $configPattern.rack) -ForegroundColor Red
        }
        else {
            # Deploy pattern to the rack servers
            Write-Host ("`nDeploy pattern to rack servers {0}" -f [string]::Join(",",$serverUuids))
            $ret = Install-LXCAConfigPattern -PatternId $pattern.Id -TargetRackServerId $serverUuids -Restart Immediate
            $ret | Format-Table -Property Uuid,Success
        }
}


# Disconnect LXCA
Disconnect-LXCA