VMware Cloud Community
electricd7
Contributor
Contributor
Jump to solution

Would like a PowerCLI script that will take a CSV as an input and do storage vmotions of all VMs

Hello-

We are in the process of replacing our storage array with a new one.  I have VMs scattered across 5 or 6 datastores and would like to be able to run a script that will process all of my VMs and send them to the correct datastore.  My CSV could contain 3 columns, VMName, SourceDS, DestinationDS.  I would like to be able to also control the number of simultaneous migrations such that I could run 4 during the day, then kill the script at night and change the code to run 10.  I have found some old scripts that do similar tasks, but they aren't compatible with vSphere 7 it seems.  Can someone help me with this?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

And that output also shows why the "wait" function is not working for you.
The Task.Description in your case says "Relocate virtual machine", while the function tests for "Apply Storage DRS recommendations" or "Migrate virtual machine"


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

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Which scripts did you find?
What exactly is not compatible with vSphere 7?


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

Reply
0 Kudos
electricd7
Contributor
Contributor
Jump to solution

Here is what I am trying, but it doesn't seem to adhere to any limit...it just fires a bunch of vmotions off until i manually kill the script:

 

Connect-VIServer vcenter

function Wait-mTaskvMotions {

[CmdletBinding()]

Param(

[int] $vMotionLimit=1,

[int] $DelayMinutes=5

)

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Apply Storage DRS recommendations|Migrate virtual machine')} | Measure-Object).Count

While ( $NumvMotionTasks -ge $vMotionLimit ) {

Write-Verbose "$(Get-Date)- Waiting $($DelayMinutes) minute(s) before checking again."

Start-Sleep ($DelayMinutes * 60)

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Apply Storage DRS recommendations|Migrate virtual machine')} | Measure-Object).Count

}

 

Write-Verbose "$(Get-Date)- Proceeding."

} # end function

$filepath = "C:\powershellscripts\MigrationVMs.csv"

$csvobj = import-csv $filepath

foreach ($row in $csvobj) {

$vmobj = get-vm $row.vmname

$ds = get-datastore $row.destds

$vmobj | move-vm -datastore $ds -confirm:$false -runasync

Wait-mTaskvMotions -vMotionLimit 4 # This will keep going through the foreach loop until 4 tasks are registered (vMotion or Storage vMotion), waits 5 minutes between checks. Will only continue to process loop when vMotion tasks are less than 4.

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you use the Verbose switch on the call to Wait-mTaskvMotions?
That might explain what is hapening.


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

Reply
0 Kudos
electricd7
Contributor
Contributor
Jump to solution

I just ran the script exactly was i copied.  Where do I add that switch?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

On the line that calls the function

Wait-mTaskvMotions -vMotionLimit 4 -Verbose


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

Reply
0 Kudos
electricd7
Contributor
Contributor
Jump to solution

OK, I will give it a shot...yea I didn't see any output on the screen from that function (i dont think)  The screen looked like the following:

 

ServerId : /VIServer=domain\me@vcenter.domain:443/
State : Queued
IsCancelable : False
PercentComplete : 0
StartTime : 1/13/2022 2:32:54 PM
FinishTime :
ObjectId : VirtualMachine-vm-1169879
Result :
Description : Relocate virtual machine
ExtensionData : VMware.Vim.Task
Id : Task-task-3180010
Name : RelocateVM_Task
Uid : /VIServer=domain\me@vcenter.domain:443/Task=Task-task-3180010/
CmdletTaskInfo : VMware.VimAutomation.Sdk.Util10.Task.ResultConvertingCmdletTaskInfoImpl


ServerId : /VIServer=domain\me@vcenter.domain:443/
State : Queued
IsCancelable : False
PercentComplete : 0
StartTime : 1/13/2022 2:32:54 PM
FinishTime :
ObjectId : VirtualMachine-vm-1169971
Result :
Description : Relocate virtual machine
ExtensionData : VMware.Vim.Task
Id : Task-task-3180011
Name : RelocateVM_Task
Uid : /VIServer=domain\me@vcenter.domain:443/Task=Task-task-3180011/
CmdletTaskInfo : VMware.VimAutomation.Sdk.Util10.Task.ResultConvertingCmdletTaskInfoImpl

 

Except there were many many more blocks before I killed it 🙂

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those are the objects returned by MOve-VM with the RunAsync switch


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And that output also shows why the "wait" function is not working for you.
The Task.Description in your case says "Relocate virtual machine", while the function tests for "Apply Storage DRS recommendations" or "Migrate virtual machine"


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

Reply
0 Kudos
electricd7
Contributor
Contributor
Jump to solution

that makes some sense...I wonder if the verbiage changed on a storage vmotion since the time that script was written?

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you are vMotion-ing to a datastore that does not use SDRS, that is the normal message afaik.


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

Reply
0 Kudos
electricd7
Contributor
Contributor
Jump to solution

Working great now.  Thanks so much!!

Reply
0 Kudos