Quickie - Scheduled Storage vMotions Using PowerCLI

155 days ago 12 views Null Patch nullpat.ch

$targetdate = "2026-01-01" $engineer = "Your Name Here" $email = "[email protected]" $vm = "EXAMPLE-VM01"

$vcentre = "yourvcentrehere.example.com" $targetdatastore = "DATASTORE-02"

$targetdisks_string = "2003,2014" $migrateconfigfile = "Y"

Connect-VIServer $vcentre

$targetdatastore_ref = (Get-Datastore $targetdatastore | Get-View).MoRef

$vm_details = Get-VM $vm $vm_view = Get-View $vm_details $alldisks = $vm_details | Get-HardDisk $targetdisks = $targetdisks_string.split(",").Trim()

$relocate_spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

if ($migrateconfigfile -like "Y*") { $relocate_spec.datastore = $targetdatastore_ref }

$disk_int = 0

if ($targetdisks) { $relocate_spec.Disk = $targetdisks | ForEach-Object { New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator } }

foreach ($disk_id in $targetdisks) { $disk_check = $alldisks | Where-Object Id -like "*/$disk_id" if ($null -eq $disk_check) { throw "$disk_id not found for VM $vm" }

$relocate_spec.Disk[$disk_int].DiskId = $disk_id $relocate_spec.Disk[$disk_int].Datastore = $targetdatastore_ref

Clear-Variable disk_check $disk_int++ }

$task_spec = New-Object VMware.Vim.ScheduledTaskSpec

$task_spec.Name