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:
- Utilize CSV file as the input; and
- 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:
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 ..
Hey, this needs a bit of trouble shooting, but based on your input, the “$._vmname” should be replaced by “$_.vmname”.