VMware Cloud Community
vin01
Expert
Expert

unregister vms from vcloud and managed over vcenter

I am trying to un-registered a specific set of vms in a Org in vCloud. The approach which I am following is below.

1.get vms in vapp for a org and poweroff

2.Do consolidation from vcloud

3.get the vcenter name for the vcloud vms

4.get the task (PromoteDisks_Task) or even like consolidation is completed for the vm

5.Once the consolidation is completed Get-AdvancedSetting -Name cloud.uuid and remove that paramater

6.change the mac address from manual to automatic

7.remove the vm from inventory

8.while registering the vm rename it to new name mentioned in $newvmname and add that vm to new resource pool mentioned in $newrsp (The new resourcepool is in same cluster)

9.Get the datastore of the vm and get the datastorecluster where that vm datastore resides and get the highest free space datastore in that cluster and do storage vm motion.( This is to change the vmfolder and vmdk file names to new display name in vcenter.

Here from the 4 step I am failing can someone help with the script.

Ex:

From a set of vms if a single vm consolidation is completed then its should proceed with next stepts 5 to 9

Its not complete script.Is it possible rebuild from start based on above steps.

$newrsp='Resp01'

$civm=Get-CIVApp 'NCompass_W2008_64Bit_Certification_NCA' -Org SEP02 |Get-CIVM

$civm |?{$_.status -eq 'PoweredOn'} |Stop-CIVM -RunAsync -Confirm:$false

$civm |ForEach-Object -Process {$_.ExtensionData.Consolidate_Task()}

(Get-View -RelatedObject $civm.extensionData)

$civm.ExtensionData.Consolidate_Task()

$vcvm= (Get-View -RelatedObject $civm.extensionData)

$tasks = @(Get-Task | where{$_.Name -eq 'PromoteDisks_Task' -and (Get-View -Id $_.ObjectId).name -eq $vcvm.Name -and 'Running','Queued' -contains $_.State})

    while ($tasks.Count -ne 0){

        sleep 5

        $tasks = @(Get-Task | where{$_.Name -eq 'PromoteDisks_Task' -and (Get-View -Id $_.ObjectId).name -eq $vcvm.Name -and 'Running','Queued' -contains $_.State})

    }

Get-VM $vcvm.Name |Get-AdvancedSetting -Name cloud.uuid |Set-AdvancedSetting ''

$dsName = $vcvm.ExtensionData.Config.Files.VmPathName.Split(']').TrimStart('[')[0]

$vmxPath = $vcvm.ExtensionData.Config.Files.VmPathName.Split(']')[1].TrimStart(' ')

$vmxName = $vmxPath.Split('/')[-1]

$vmxFolder = [string]::Join('/', ($vmxPath.Split('/')[-99..-2]))

$newvmname= @()

for($i=1; $i -le 110; $i++){

$newvmname+=New-VM -Name "$('LIBFORTPERFTEST0')$($i)" -VMFilePath $vmxPath -ResourcePool $newrsp

}

Regards Vineeth.K
0 Kudos
7 Replies
LucD
Leadership
Leadership

Do you get an error?
Can you show it?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Hi LucD,

No Error. When I get the task in vcenter with the name 'PromoteDisks_Task' for the vms its not going to next line even after disk consolidation is done in vcenter.

$tasks = @(Get-Task | where{$_.Name -eq 'PromoteDisks_Task' -and (Get-View -Id $_.ObjectId).name -eq $vcvm.Name -and 'Running','Queued' -contains $_.State})

    while ($tasks.Count -ne 0){

        sleep 5

        $tasks = @(Get-Task | where{$_.Name -eq 'PromoteDisks_Task' -and (Get-View -Id $_.ObjectId).name -eq $vcvm.Name -and 'Running','Queued' -contains $_.State})

    }

I have not build the script completely based on the above mentioned steps can you please provide the script.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

It looks as if you never get out of that loop.
Can you add some debugging lines?
For example, show the count the variable has.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Let me explain this way.

I am providing the list of vcloud vms from a csv and checking the powerstate and if it is poweredon then powering off and proceeding with Consolidate_Task().

Next line I can able to fetch the vcenter VMname for the vcloud vms.

Now with the vcenter VM name i can see the events with (FullFormattedMessage : Task: Promote virtual machine disks) by using Get-VIEvent -Entity $vcvm.Name

so here I can see multiple vms with disk Consolidation in process. If a vm completes its disk Consolidation(or vmevents shows disk consolidation task succeed or complete) then its should proceed with next steps below

5.Get-AdvancedSetting -Name cloud.uuid and remove that paramater

6.change the mac address from manual to automatic

7.remove the vm from inventory

8.while registering the vm rename it to new name mentioned in $newvmname and add that vm to new resource pool mentioned in $newrsp (The new resourcepool is in same cluster)

9.Get the datastore of the vm and get the datastorecluster where that vm datastore resides and get the highest free space datastore in that cluster and do storage vm motion.( This is to change the vmfolder and vmdk file names to new display name in vcenter.

$vcloudvms= Import-Csv -Path C:\Users\vk185112\Desktop\alpwindowsvmsinfo_feb01_2019.csv -UseCulture

$civm=Get-CIVM $vcloudvms.Name

$civm |?{$_.status -eq 'PoweredOn'} |Stop-CIVM -Confirm:$false

$civm |ForEach-Object -Process {$_.ExtensionData.Consolidate_Task()}

$vcvm= (Get-View -RelatedObject $civm.extensionData)

$eventTypes = Get-VIEvent -Entity $vcvm.Name  | % {$_.GetType().FullFormattedMessage}

pastedImage_1.png

pastedImage_0.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

Do these PromoteDisks tasks ever get out of queued?
Or do they just take a long time?

Still not sure what you are asking for in fact.
Is it the code for steps 5-9 that you are looking for?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Do these PromoteDisks tasks ever get out of queued?

Or do they just take a long time?

The Promotedisk tasks takes long time to complete(Its expected). So For example- If 5 vms started consolidation then 2nd & 3rd Vms completed the consolidation first then it should go with the next lines in the script as below and go back to the loop and wait for the next vms to finish the consolidation

5.Once the consolidation is completed Get-AdvancedSetting -Name cloud.uuid and remove that paramater

6.change the mac address from manual to automatic

7.remove the vm from inventory

8.while registering the vm rename it to new name mentioned in $newvmname and add that vm to new resource pool mentioned in $newrsp (The new resourcepool is in same cluster)

9.Get the datastore of the vm and get the datastorecluster where that vm datastore resides and get the highest free space datastore in that cluster and do storage vm motion.( This is to change the vmfolder and vmdk file names to new display name in vcenter.

Still not sure what you are asking for in fact.

I am sorry for creating confusion.I am clear with the process which I am doing through UI.

Is it the code for steps 5-9 that you are looking for?

yes I do request to provide the code for step 5 to 9

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert

Hi LucD,

I am trying to split the script in two parts. Is it possible to get the code for below tasks.

1.I have a set of vms in a csv file.

2.Need to remove cloud.uuid in advanced settings

3.Need to get vmx file path for that vms

4.remove the vm from inventory

5.Readd the vm as a new vm(New vm name avaliable in csv file) using vmx file path on step2 on a new resourcepool(new resourcepool name is available in csv file)(The new resourcepool is in same cluster)

6.change the mac address from manual to automatic for that vm

7.Get the datastore of the vm and get the datastorecluster where that vm datastore resides and get the highest free space datastore in that cluster and do storage vm motion.( This is to change the vmfolder and vmdk file names to new display name in vcenter.

Csv file:

pastedImage_2.png

Regards Vineeth.K
0 Kudos