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.
functionprepare_for_snapshot
{
param (
$virtualMachine,
$datastoreCluster
)
$vm=Get-VM$virtualMachine|SelectName, @{N=”Datastore”;E={[string]::Join(‘,’,(Get-Datastore-Id$_.DatastoreIdList |Select-ExpandPropertyName))}}
$currentDatastore=$vm.Datastore
$harddisks=Get-HardDisk$virtualMachine
$hardDiskLocations=@()
foreach($hddin$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-TypeNameVMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.SdrsVMDiskAntiAffinityRule-ArgumentList$harddisks
Move-VM-VM$virtualMachine-Datastore$targetDatastoreCluster-AdvancedOption$vmkdAntiAffinityRule
}
functionfinish_snapshot_procedure
{
param (
$virtualMachine
)
$hardDiskLocations=Import-Csv$virtualMachine".csv"
$harddisks=Get-HardDisk$virtualMachine
$x=1;
foreach($hddin$harddisks)
{
Move-HardDisk-HardDisk$hdd-Datastore$hardDiskLocations[$x-1].Datastore -Confirm:$false
}
}