I'm creating a procedure that will move a vm to a set of datastores, then take snapshots on the storage side, and then move the storage back. The first one exports a csv with the hard disk number and datastore location so I can put them back. It works, but it doesn't move the vmx file, how can I move just that without having to move all the disks.
function prepare_for_snapshot
{
param (
$virtualMachine,
$datastoreCluster
)
$vm=Get-VM $virtualMachine | Select Name, @{N=”Datastore”;E={[string]::Join(‘,’,(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}
$currentDatastore=$vm.Datastore
$harddisks=Get-HardDisk $virtualMachine
$hardDiskLocations=@()
foreach($hdd in $harddisks)
{
$previousLocation=[PSCustomObject]@{
Name=$hdd.name
Datastore=$hdd.Filename.Split(']')[0].TrimStart('[')
}
$hardDiskLocations+=$previousLocation
}
$hardDiskLocations | Export-Csv $virtualMachine".csv" -UseCulture -NoTypeInformation
$targetDatastoreCluster=Get-DatastoreCluster -Name $datastoreCluster
$vmkdAntiAffinityRule=New-Object -TypeName VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.SdrsVMDiskAntiAffinityRule -ArgumentList $harddisks
Move-VM -VM $virtualMachine -Datastore $targetDatastoreCluster -AdvancedOption $vmkdAntiAffinityRule
}
function finish_snapshot_procedure
{
param (
$virtualMachine
)
$hardDiskLocations=Import-Csv $virtualMachine".csv"
$harddisks=Get-HardDisk $virtualMachine
$x=1;
foreach($hdd in $harddisks)
{
Move-HardDisk -HardDisk $hdd -Datastore $hardDiskLocations[$x-1].Datastore -Confirm:$false
}
}
Figures I find it after searching the exact question
Move configuration files with PowerCLI?
I think I can use this
Get-VM | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”VM Path”}
to save the vmx location and then use the code to the link to move it back
For anyone that find this, this works. The prepare function will move the vm to a datastore and spread the disks out accorss all the datastores and the finish function will place the vm in its orginal location
function prepare_for_snapshot
param (
$virtualMachine,
$datastoreCluster
$vm=Get-VM $virtualMachine | Select Name, @{N=”Datastore”;E={[string]::Join(‘,’,(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}
$currentDatastore=$vm.Datastore
$harddisks=Get-HardDisk $virtualMachine
$hardDiskLocations=@()
$vmlocation=Get-VM $vmName | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”VM Path”}
$vmxDatastore=$vmlocation.'VM Path'.Split(']')[0].TrimStart('[')
$fileLocation=[PSCustomObject]@{
='VMX'
=$vmxDatastore
$hardDiskLocations+=$fileLocation
foreach($hdd in $harddisks)
$previousLocation=[PSCustomObject]@{
=$hdd.name
=$hdd.Filename.Split(']')[0].TrimStart('[')
$hardDiskLocations+=$previousLocation
$hardDiskLocations | Export-Csv $virtualMachine".csv" -UseCulture -NoTypeInformation
$targetDatastoreCluster=Get-DatastoreCluster -Name $datastoreCluster
$vmkdAntiAffinityRule=New-Object -TypeName VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.SdrsVMDiskAntiAffinityRule -ArgumentList $harddisks
Move-VM -VM $virtualMachine -Datastore $targetDatastoreCluster -AdvancedOption $vmkdAntiAffinityRule
finish_snapshot_procedure
param (
$virtualMachine
$hardDiskLocations=Import-Csv $virtualMachine".csv"
$vm=Get-VM $virtualMachine
$vxmLocation=$hardDiskLocations[0].Datastore
$harddisks=Get-HardDisk $virtualMachine
$spec=New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Datastore=(Get-Datastore -Name $vxmLocation).ExtensionData.MoRef
$harddisks | %{
$disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$disk.diskId = $_.Extensiondata.Key
$disk.datastore = $_.Extensiondata.Backing.Datastore
$spec.disk += $disk
$vm.Extensiondata.RelocateVM_Task($spec, "defaultPriority")
$x=1
foreach($hdd in $harddisks)
Move-HardDisk -HardDisk $hdd -Datastore $hardDiskLocations[$x].Datastore -Confirm:$false
$x++