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:

CSV1

The output of script is as below:

Output2

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s