winsolo
Enthusiast
Enthusiast

Rename and Backup VM vmx file

Jump to solution

Is there a way to rename a vmx file by appending a string to the vmx filename in the same VM folder?

PS C:\> (Get-VM TestVM).ExtensionData.Config.Files


VmPathName          : [vSAN-Compute-Cluster] c224f15d-f813-5dc8-24ac-e4434b62be10/TestVM.vmx

SnapshotDirectory   : [vSAN-Compute-Cluster] c224f15d-f813-5dc8-24ac-e4434b62be10/

SuspendDirectory    : [vSAN-Compute-Cluster] c224f15d-f813-5dc8-24ac-e4434b62be10/

LogDirectory        : [vSAN-Compute-Cluster] c224f15d-f813-5dc8-24ac-e4434b62be10/

FtMetadataDirectory :

I'd like to be able to copy the renamed file in the same folder while keeping the original .vmx file in the same LogDirectory folder. I looked at PowerCLI Script to Backup all VMX files but the backup destination is set to the local temp folder which isn't what my requirement is.

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Something like this?

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$vmx = $vm.ExtensionData.LayoutEx.File | where{$_.Type -eq 'config'}

$src = $vmx.Name

$dst = $src -replace '.vmx','_suffix.vmx'

$dc = Get-Datacenter -VM $vm


$fileMgr = Get-View FileManager

$fileMgr.CopyDatastoreFile($src,$dc.ExtensionData.MoRef,$dst,$dc.ExtensionData.MoRef,$false)


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

View solution in original post

3 Replies
LucD
Leadership
Leadership

Something like this?

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$vmx = $vm.ExtensionData.LayoutEx.File | where{$_.Type -eq 'config'}

$src = $vmx.Name

$dst = $src -replace '.vmx','_suffix.vmx'

$dc = Get-Datacenter -VM $vm


$fileMgr = Get-View FileManager

$fileMgr.CopyDatastoreFile($src,$dc.ExtensionData.MoRef,$dst,$dc.ExtensionData.MoRef,$false)


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

View solution in original post

winsolo
Enthusiast
Enthusiast

LucD would you mind helping me with modifying it to run against multiple VMs, please? I tried but I'm having problems with substituting the variables for the 2nd VM in the ForEach loop.

0 Kudos
LucD
Leadership
Leadership

You could do something like this

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    $vmx = $vm.ExtensionData.LayoutEx.File | where{$_.Type -eq 'config'}

    $src = $vmx.Name

    $dst = $src -replace '.vmx','_suffix.vmx'

    $dc = Get-Datacenter -VM $vm

    $fileMgr = Get-View FileManager

    $fileMgr.CopyDatastoreFile($src,$dc.ExtensionData.MoRef,$dst,$dc.ExtensionData.MoRef,$false)

}


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