VMware Cloud Community
CCSL
Contributor
Contributor

Modify VMX Datastore UUID with PowerCLI

Scenario:

I've replicated a bunch of Datastores containing vm's from my protected vCenter to my recovery vCenter for disaster recovery purposes. I have seperate datastores for my windows pagefiles that I wont be replicating. I don't have budget to use SRM.

The VMX files for the vm's at my recovery vcenter contain datastore UUID's for the pagefile virtual disk that I won't be replicating. I'm using vmkfstools to clone pagefile disks to a new datastore at the recovery site.

I can script obtaining the UUID's and adding the VMX files to inventory.

Question:

Is it possible to use PowerCLI to automate effectively performaing a find/replace within each VMX file with the old/new datastore UUID's for the pagefile disk? If so does anyone know of an existing script to get me started?

0 Kudos
11 Replies
RvdNieuwendijk
Leadership
Leadership

I am not sure which line in the vmx files you want to modify. So I will give you an example of an unrelated line "mem.hotadd" just to show you how you can change a line in a vmx file:

Get-VM -Name YourVM |
Get-Advancedsetting -Name mem.hotadd |
Set-AdvancedSetting -Value $true -Confirm:$false

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
CCSL
Contributor
Contributor

The line I need to modify is :

scsi0:2.filename

Not sure this is configurable with Get-AdvancedSetting/Set-AdvancedSetting ?

0 Kudos
LucD
Leadership
Leadership

No, afaik you can't do this by changing the VMX file.

Did you try to remove and add that specific harddisk for the VMs involved ?


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

0 Kudos
CCSL
Contributor
Contributor

LucD,

If I add the vm to the inventory in the recovery site I'm unable to remove the harddisk, I guess because it doesn't know where the disk is as the underlying datastore is not present. The only option seems to be manipulating the vmx file by removing the additional disk or updating the UUID with a new disk.

0 Kudos
LucD
Leadership
Leadership

I see, is the SCSIx.File entry avialble through the Get-AdvancedSetting cmdlet ?


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

0 Kudos
CCSL
Contributor
Contributor

I don't believe so 😞

0 Kudos
LucD
Leadership
Leadership

Can you remove the harddisk with this script ?

Update the VMname and the harddiskname

$tgtHD = "Hard disk 2" 
$vmName
= "MyVM"
$vm
= Get-VM -Name $vmName $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev
= New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.Operation = "remove"
$dev.device = $vm.ExtensionData.Config.Hardware.Device |
  where {$_ -is [VMware.Vim.VirtualDisk] -and $_.DeviceInfo.Label -eq $tgtHD} $spec.DeviceChange += $dev
$vm
.ExtensionData.ReconfigVM($spec)


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

0 Kudos
CCSL
Contributor
Contributor

No, not for these cases where the disk does not exist. 'Invalid configuration for device'.

0 Kudos
LucD
Leadership
Leadership

I was afraid of that Smiley Sad

Did you try to start one fo those replicated VMs ?

Do they come up with a question ?

If yes, you can try to use the Get-VMQuestion and Set-VMQuestion to answer the question.


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

0 Kudos
CCSL
Contributor
Contributor

No - 'Virtual Hard Disk x is not acessible on the host: Unable to access file [] Datastore/vm/vm.vmdk 😞

0 Kudos
LucD
Leadership
Leadership

Afaik you can't remove lines from the VMX through an API, but you can blank them out.

See How to remove an extraConfig setting

Could you try that for all the scsi0:2.x entries ?

Obviously the harddisk will be gone this way, but perhaps that would allow you to poweron the VM.

And you can add a new harddisk later on via a script.


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

0 Kudos