I have adapted a powershell script to login to a vcenter with admin credentials then iterate through all the VMs sequentially on a particular datastore(A) and move them to datastore(B) against a Vcenter 6 appliance and a ESXI 6 host.
The script ensure that the Storage Vmotion for a VM is complete before it iterates to the next VM in the list
The script works successfully for the the first VM to be processed but it is not signalling that the Storage Vmotion for the first VM is complete. So the script never moves on to the second VM in the list.
- What could be the error stopping iteration through the list of Vms?
- The script is a few years old. Is there a better way to do this currently?
# Customized from script found at https://communities.vmware.com/thread/455209
# Script uses the cmdlet 'Storage-Vmotion' from the above thread and cmdlet 'Get-VIeventPLus' found at
# http://www.lucd.info/2013/03/31/get-the-vmotionsvmotion-history/
# Load PowerCLI cmdlets
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue"
# Define vCenter User and target Datastore
$vcHost = 'VCENTER'
$vcUser = 'VCENTER_ADMIN'
$vcPass = 'PASSWORD'
$svmSource = 'SOURCE_DATASTORE'
$svmTarget = 'DESTINATION_DATASTORE'
# Ignore is there is an invalid certificate (this is for use in the lab and not production)
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
# Connect to vCenter
Connect-VIServer $vcHost -User $vcUser -Password $vcPass
# Get VMs (pass array of VMs to $VMs from the source datastore)
$VMs = get-datastore $svmSource | Get-VM
Write-Host $VMs
Storage-Vmotion -VMname $VMs -DesitinationDatastore $svmTarget