Automation

 View Only

 powershell code for cluster "images" update tab in vCenter

Jump to  Best Answer
jonebgood_157's profile image
jonebgood_157 posted Oct 28, 2024 10:02 AM

So, I use the cluster level images on vcenter 8.x, but the problem is that setting the baseline image and vendor add-on is a manual process. Well, I have one vCenter with about 100 clusters with 2-3 hosts in each of them. This is not an ideal process to click each one and set it, and then to remediate, I have to click to kick off each one. Here is the challenge and my questions. I'm not finding much on the api for this function/feature.

1) Can I powershell the setting of each "cluster image"; ideally, I'd like to use an input .csv files in case I want to control which clusters to set

2) If I can do #1 above, then I'd like to kick off the remediation feature of each cluster image, once again ideally use an input file

new_bember's profile image
new_bember  Best Answer

Hello @jonebgood_157 

  1. Import-LcmClusterDesiredState -Cluster $cluster -LocalSpecLocation .\cluster_desired_state.json
  2. $cluster | set-cluster -Remediate -AcceptEULA

You can export json from any cluster and edit it manually or with powershell, then re-use it.

jonebgood_157's profile image
jonebgood_157

@new_bember

Thanks! This looks promising. Have you tried it with importing multiple clusters(lets say 10 clusters from a csv as an input file)?

new_bember's profile image
new_bember

@jonebgood_157
Yep, imported json to up to 40 clusters at the same time. And you will probably hit Java OOM for vapi-endpoint service if you will start to remediate all 40 cluster at once, so better to do it in smaller batches.

My case is easier a bit, I have same image for all clusters, so use just single json file.

jonebgood_157's profile image
jonebgood_157

@new_bember Did you just use a function to input the clusters from a text file and then loop through "for each $cluster in $clusters"?  or something like that

new_bember's profile image
new_bember

@jonebgood_157 in my setup Jenkins do: Get-Cluster | ? {$_.Name -notmatch 'Deploy|Temp|Spare'} on one stage and save output to file (txt/csv doesn't matter), and then remediate all clusters in parallel stage (but actually works with single cluster in stage scope)

jonebgood_157's profile image
jonebgood_157

@new_bember

Ive been using this and got the remediation going but the issue now is that the remediation runs sequentially.  Do you know how I can kickoff multiple remediations at once? For example, my cluster input file has 5 clusters; here is some of my code minus comments and logging, etc.

# Get vCenter ClusterName from txt file
$clusters = Get-Content $VIClusters | %{Get-Cluster $_}

# Import Cluster Image Desired State
Function SetClusterImage ($CurrentCluster) {
  # Get vCenter Cluster Name
    $ClusterName = $CurrentCluster.Name
 
 #Remediate Cluster with set Image
    Write-Host "** Remediating Cluster Image for $ClusterName **"
    $CurrentCluster | set-cluster -Remediate -AcceptEULA

new_bember's profile image
new_bember

@jonebgood_157

Actually it should runs sequentially, but all clusters at the same time, 1 host per cluster.

As I mentioned before, in Jenkins you can do this with parallel stages. As for powershell, I think in powershell 7 there is an option -Parallel for foreach command. 

Or for example you can wrap your code in a function and call it in some FOR loop, like:

for($i=1, $i -le $cluster.count, $i++){

  some_trick with Start-Job cmdlet, that allow you to run function in background.

}