VMware Cloud Community
sorc9
Contributor
Contributor

Reposition VM

I am looking to delete VM that is created and reposition.

 

0 Kudos
13 Replies
LucD
Leadership
Leadership

The New-Timespan cmdlet is ideal for comparing two

$t1 = (Get-Date).AddDays(-21)
$t2 = Get-Date

(New-Timespan -Start $t1 -End $t2).TotalDays

# To test
(New-Timespan -Start $t1 -End $t2).TotalDays -gt 20

dates.
For example



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

0 Kudos
sorc9
Contributor
Contributor

Thank you in advance. 

As for comparing created dates for each template, what would be the optimal way?

0 Kudos
LucD
Leadership
Leadership

I don't think the ChangeVersion will give you the creation date of a Template.

I would look at the events (provided you keep those long enough on your VCSA).
Something like this.
it looks back 1 month and reports all templates older than 20 days.
Note that the Get-VIEvent cmdlet might take quite a bit of time to complete.

 

$now = Get-Date
$start = $now.AddMonths(-1)
$maxDays = 20

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) |
Where-Object { $_ -is [VMware.Vim.EventEx] -and $_.EventTypeId -eq 'com.vmware.vc.vm.VmConvertedToTemplateEvent' } |
ForEach-Object -Process {
  if ((New-TimeSpan -Start $_.CreatedTime -End $now).TotalDays -gt $maxDays) {
    New-Object -TypeName PSObject -Property ([ordered]@{
        Template = $_.ObjectName
        Created = $_.CreatedTime
        AgeInDays = [int](New-TimeSpan -Start $_.CreatedTime -End $now).TotalDays
      })
  }
}

 

This only lists the templates, adding a delete step should be simple


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

0 Kudos
sorc9
Contributor
Contributor

Thank you

0 Kudos
LucD
Leadership
Leadership

You could use my Get-VIEventPlus function.
That allows specifying the entity and  eventype, meaning that the filtering already happens on the VCSA vs returning all events to the script.
Retrieving the events would be like this (and should be a lot faster)

$entity = Get-Template
Get-VIEventPlus -Entity $entity -EventType 'com.vmware.vc.vm.VmConvertedToTemplateEvent' -Start (Get-Date).AddMonths(-1)

 


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

0 Kudos
sorc9
Contributor
Contributor

Question

1) Does it support operation with 1 cmdlet

0 Kudos
LucD
Leadership
Leadership

A word of warning, if the same Template was created for example 30 days ago, then removed, and then recreated 10 days ago, the name of the template will be listed. You will have to check something like the MoRef of the Template to make sure you are removing the correct one.

1) Not sure I understand the question 100%, but if it is what I think it, you can use a WHer-clause on the Get-Template, and then pass only those on the Entity parameter

2) See my remark above. 

Yes, the Remove-Template cmdlet accepts an array of Templates, so you can remove all of them in 1 call to the cmdlet.


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

0 Kudos
sorc9
Contributor
Contributor

Thanks!! For deleting, I'd take it you mean to make 1 call to the cmdlet.

 

0 Kudos
LucD
Leadership
Leadership

You need to pipe the result to a Where clause that filters on that condition


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

0 Kudos
sorc9
Contributor
Contributor

Thank you. I'll look at it. Does output an error.

0 Kudos
LucD
Leadership
Leadership

That function also has a Finish parameter.
What errors do you get?


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

0 Kudos
sorc9
Contributor
Contributor

I got it to work. Thanks. What do you mean to pipe it out?

0 Kudos
LucD
Leadership
Leadership

You could do

$entity = Get-Template
Get-VIEventPlus -Entity $entity -EventType 'com.vmware.vc.vm.VmConvertedToTemplateEvent' -Start (Get-Date).AddDays(-30) -Finish (Get-Date).AddDays(-20) |
ForEach-Object -Process {
  Get-Template -Name $_.ObjectName | Remove-Template -Confirm:$false 
}

 


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

0 Kudos