VMware Cloud Community
Netxe
Contributor
Contributor

Need help with script that will extract the vm OVF and it's snapshots

I gave GPTchat a go and asked it to make script that will export OVFs and snapshots but it seems it getting stuck on  $snapshotPath = $snapshot.ExtensionData.Config.Files.SnapshotDirectory any clue how im supposed to fix it?

here's the full code:

 

# Import the PowerCLI module
Import - Module VMware.PowerCLI

Connect-viserver $vCenter -user $vCenterUser -password $vCenterPassword

  # Read the list of VMs from the text file
$vmList = Get-Content -Path ".\list.txt"

  # Loop through each VM in the list
foreach($vm in $vmList) {
  # Get the VM object
  $vmObject = Get-VM -Name $vm

  New-Item -ItemType Directory -Path "path" \$vm

  # Export the VM
  Export-VM -Name $vm -Destination "pathn" \$vm

  # Copy the snapshot files of the VM
  $snapshots = Get - Snapshot - VM $vmObject
  foreach($snapshot in $snapshots) {
    $snapshotPath = $snapshot.ExtensionData.Config.Files.SnapshotDirectory
    $snapshotName = $snapshot.Name
    Copy-Item -Path $snapshotPath\ $snapshotName - Destination "path" \$vm
  }
}

# Disconnect from the vCenter server
Disconnect - VIServer - Server * -Confirm: $false

 

 

 

0 Kudos
5 Replies
LucD
Leadership
Leadership

I already wondered when the first post with ChatGPT code would appear here. 😂

It shows how clueless this AI (?) is about working code.
It just assembles snippets of code, which it thinks will produce a working script.
To take away the suspense, it doesn't produce working code in most cases.

The ExtensionData.Config.Files.SnapshotDirectory property exists on a VirtualMachine object (returned by Get-VM), not on a Snapshot object (returned by Get-Snapshot).
Also, on a VirtualMachine object, the Snapshots are linked together (they are delta files after all) via the Extension.Snapshot property with a series of VirtualMachineSnapshotInfo objects.

Second, and more important error, you can't copy a Snapshot (which consists of multiple files) with a Copy-Item cmdlet.
The VMDK files in a Snapshot are each at least 2 files, and those files are not copied correctly with Copy-Item.
If you really want to copy such files, have a look at the Copy-DatastoreItem cmdlet.
But again the copied VMDK files will not be usable since Snapshots are in fact delta files.

So, asking for something that makes no sense (exporting snapshots) gives you a script that makes no sense I'm afraid



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

Netxe
Contributor
Contributor

Thanks for the replay, yea I know that the script is quite broken as even I can see where it's gonna fail, but for the reason of why I need to export the snapshots is because the program I'm using in my work utilizing the snapshot faction of esxi as config file for a vm machine.

So i need to make a script that will export the OVF of vms and it's delta files so i can transfer it to another esxi machine, And I'm clueless on how you suppose to extract the delta files with powerclii, if you could tell me how i supposed to do it? would be nice, as there's not much information on google about this subject.

 

0 Kudos
LucD
Leadership
Leadership

What exactly do you mean by "... utilizing the snapshot faction of esxi as config file for a vm machine"


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

0 Kudos
Netxe
Contributor
Contributor

Each snapshot got slight different settings for our program to utilize that machine, so now we trying to find way to export this machine OVF and it snapshots so we can move it to the different machine that has the same program but needs the VM and snapshots

0 Kudos
LucD
Leadership
Leadership

If you mean cloning a VM, that can be done with the New-VM cmdlet and the CloneVM parameter set.

Manipulating snapshots in the way I think you envisage is not really feasible I'm afraid, see KB1015180.
Take note of the Caution part in that KB.

snap.jpg

Commercial Backup/Restore SW uses Snapshots to create a backup of a VM.
And they allow you create a new VM from that backup.


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