PowerCLI Script – VMware Storage vMotion Automation

I recently got a task to migrate more than 100 VMware virtual machine from one sets of datastores to another. In order to automate this process, I draft a simple PowerShell script to perform below tasks:

  1. Utilize CSV file as the input; and
  2. The maximum running parallel storage vMotion sessions can be controlled by input option, this is to avoid storage overhead and minimize the impact on the other running applications.

The script is as below:

$csvinput = $args[0]
$maxsession = $args[1]
$runsession = 0

echo "---------------Summary----------------"
echo "This script is to perform the Virtual Storage vMotion with give max sessions"
echo "--------------------------------------"
import-csv $csvinput | foreach {

echo "------------------------------------------------"
echo "Checking the running vMotion now.... Pls wait...."
echo "------------------------------------------------"

Do {

$runsession = (get-task | where {$_.name -like "RelocateVM_Task" -and $_.State -eq "Running" }).count

if ($runsession -ge $maxsession) {
  echo "The current running vMotion sessions is $runsession.No new vMotion will be started.Next check will be performed in 1 minute."
  Start-Sleep -s 60
  get-task | where {$_.State -eq "running"}

else {
  echo "The current running vMotion sessions is $runsession, a new storage vMotion will be started soon."
  Start-Sleep -s 5

} While ( $runsession -ge $maxsession)

echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo "The Storage vMotion for will start for below VM ..."
echo $_.vmname
echo $_.targetds
Get-VM $_.vmname | Move-VM -Datastore $_.targetds -RunAsync -Confirm:$false
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"


Below is the example of command execution:

./My_storagevmotion.ps1   c:\temp\vmlist.csv  4

*4 is the max running storage vMotion sessions, could be other value.

The csv file example is as below:


The output of script is as below:


2 thoughts on “PowerCLI Script – VMware Storage vMotion Automation

  1. filland

    Hey I have some issue’s getting this script to work can you please help?

    “Get-VM VM with name ‘$._vmname’ was not found using the specified filter(s).
    At C:\temp\storagevm.ps1:19 char:2”
    I have created an csv file with 2 columns ,one with vmname and a second with targetds , just like your example..
    Do you have any idea why this is not working ?
    Thank you ..


Leave a Reply