VMware Cloud Community
2kool4skool
Contributor
Contributor

Excluding Resource Pool From Script (Snapshot Removal)

Hi All,

I have the below script that has been working well in our test environment. I would like to deploy it to our production vcentre instance.

Get-VM  | `

  Get-Snapshot |  `

  Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } | `

  Remove-Snapshot

I have a need to exclude resource pools that are associated with our View environment. Obviously we do not want to be removing snapshots from the template images for Horizon View.

Is this possible? If so, how would I go about it? I have been unable to find a solid answer on adding an exclusion to a powershell script.

Thanks

0 Kudos
14 Replies
LucD
Leadership
Leadership

One solution could be

Get-ResourcePool | Where {$_.Name -ne "ExcludedResourcePool"} |

Get-VM  |

  Get-Snapshot |

  Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } |

  Remove-Snapshot


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

0 Kudos
2kool4skool
Contributor
Contributor

Thank you for that!

So if I understand correctly, that is in fact excluding the resource group specified in brackets? If I wanted to specify more then one resource group, would I separate them with a comma or semi-colon?

0 Kudos
LucD
Leadership
Leadership

When you want to be able to exclude more than 1 resourcepool, I would use another operator.

Something like this

Get-ResourcePool | Where {"rp1","rp2","rp3" -notcontains $_.Name} |

Get-VM  |

  Get-Snapshot |

  Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } |

  Remove-Snapshot

This would exclude resourcepools rp1, rp2 and rp3


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

0 Kudos
2kool4skool
Contributor
Contributor

Very helpful, thank you!

I will test this out and post back on my results.

0 Kudos
dlepinski
Contributor
Contributor

Sorry to dig up an old thread, but this is exactly the situation I'm trying to solve.

Did LucD suggestion work for you, 2kool4skool I tried the script below, but it doesn't seem to be excluding the Normal and Low resource pools.

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer -Server myserver

Get-ResourcePool | Where {"2-Normal","3-Low" -notcontains $_.Name} | `

  Get-VM | `

  Get-Snapshot |  `

  Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

  Remove-Snapshot -Confirm:$False -Whatif


I don't know if something changed over the last couple years, but I can't seem to get it to exclude my resource pools. I ran Get-ResourcePool -Name -2-Normal in PowerCLI to make sure it's seeing the 2-Normal resource pool, and it is. So it would seem I have everything in place correctly.

Twitter: @dlepi
0 Kudos
LucD
Leadership
Leadership

Correct, and you could then use the -notcontains operand.

Get-ResourcePool | Where {"ExcludedResourcePool1","ExcludedResourcePool2" -notcontains $_.Name} |

Get-VM  |

  Get-Snapshot |

  Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } |

  Remove-Snapshot


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

0 Kudos
dlepinski
Contributor
Contributor

Here's the output I'm seeing:

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer -Server myvcenter

Get-ResourcePool | Where {"2-Normal","3-Low" -notcontains $_.Name} | `

  Get-VM | `

  Get-Snapshot |  `

  Where-Object { $_.Created -lt (Get-Date).AddDays(-0) } | `

  Remove-Snapshot -Confirm:$False -Whatif

Name                           Port  User                         

----                           ----  ----                         

myvcenter                 443   myusername   

What if: Performing the operation "Removing snapshot." on target "VirtualMachineSnapshot-snapshot-249".

What if: Performing the operation "Removing snapshot." on target "VirtualMachineSnapshot-snapshot-247".

What if: Performing the operation "Removing snapshot." on target "VirtualMachineSnapshot-snapshot-248".

Each of the 3 test snapshots is in a different resource pool. If I'm understanding correctly, 2 of the 3 should not be there as I'm excluding them. I've tested without the -Whatif and all 3 snapshots are in fact getting deleted. So something doesn't seem to be quite right.

Thank you in advance for the help!

Twitter: @dlepi
0 Kudos
LucD
Leadership
Leadership

Can you do the following check ?

It should only list VMs that are not in the Resource Pools "2-Normal" and "3-Low".

Get-ResourcePool | Where {"2-Normal","3-Low" -notcontains $_.Name} |

Get-VM |

Select Name,

    @{N='ResourcePool';E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}


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

0 Kudos
dlepinski
Contributor
Contributor

Hmmm... the output listed all VMs in all three resource pools.

Twitter: @dlepi
0 Kudos
LucD
Leadership
Leadership

Perhaps a stupid question, but are these resourcepool names correct ?

What does the resulting output show ?


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

0 Kudos
dlepinski
Contributor
Contributor

This is the output I get.

Get-ResourcePool | Where {"2-Normal","3-Low" -notcontains $_.Name} |

Get-VM |

Select Name,

    @{N='ResourcePool';E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}

Name                                                                                            ResourcePool                                                                                 

----                                                                                            ------------                                                                                 

Server1                                                                                  3-Low                                                                                        

Server2                                                                                3-Normal                                                                                        

Server3                                                                                   3-High             

I also ran Get-ResourcePool -Name 2-Normal and saw the expected output. I can do that for all 3 resource pools successfully.

Twitter: @dlepi
0 Kudos
LucD
Leadership
Leadership

The numbers at the beginning of the resourcepool names don't seem to correspond.


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

0 Kudos
dlepinski
Contributor
Contributor

Removing the numbers and dash doesn't improve the results. Running it as this gives me the same output; lists all servers in all resource pools.

Get-ResourcePool | Where {"Normal","Low" -notcontains $_.Name} |

Get-VM |

Select Name,

    @{N='ResourcePool';E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}

However, if I run Get-ResourcePool -Name "Normal" it will fail saying that it can't find resource pool 'Normal'. I assume the above script work because it's looking for resource pools containing those names.

Twitter: @dlepi
0 Kudos
dlepinski
Contributor
Contributor

Additionally, if I comment out Get-VM I only see the 1-High resource pool. So I must need something more to go along with Get-VM?

Twitter: @dlepi
0 Kudos