VMware Cloud Community
Kesh111
Contributor
Contributor
Jump to solution

Automate snapshots based on custom field

I'm a newbie to scripting and am having trouble creating a Powershell script that will automatically create snapsht based on a custom attribute that is specified on VM. I have got this far:

Get-VM | Select Name -ExpandProperty Custom Fields | Where {$_.value -eq "takesnapshot"} | Select Name

This gives me a list of VM names that have the custom field "takesnapshot" specified, however, I am not sure how to use the output with the Get-VM | New-Snapshot command to create a new snapshot. Any help would be appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, the Key is "department" and the Value is "snapshot".

With the attached line you should get it running.

Your test had a few typos: it should be $_ and you should leave out the blank before the square bracket.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

You could try

Get-VM | where {$_.CustomFields -eq "takesnapshot"}  | New-Snapshot -Confirm:$false

I attached the line in a file since there are some square brackets in there.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Kesh111
Contributor
Contributor
Jump to solution

Hi LucD,

I have tried that unfortunately does not seem to work. When using the Get-VM | Select Name -ExpandProperty Custom Fields I get two columns, one called "Key" and the other "Value". The "Value" coulmn contains the custom attribute that I have written. Therefore using your code I get the attached.

I have also tried $_.Value and "Value" in the square brackets with no luck. Sorry if I'm missing something obvious but I'm struggling to figure this out.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you do the following for one of the guests that has the custom field defined

(Get-VM <name-of-the-vm>).CustomFields

This should display 2 columns, one marked Key the other one marked Value.

It's the value under the Key header that you have to use between the square brackets.

Include a screenshot of the output if you have trouble finding what I mean.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Kesh111
Contributor
Contributor
Jump to solution

Please see the output I'm getting.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, the Key is "department" and the Value is "snapshot".

With the attached line you should get it running.

Your test had a few typos: it should be $_ and you should leave out the blank before the square bracket.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Kesh111
Contributor
Contributor
Jump to solution

Thanks a lot, that works now.

0 Kudos
Kesh111
Contributor
Contributor
Jump to solution

Thanks a lot, that works now.

0 Kudos