Hi All,
I am planning to migrate all VMs from one old datastore to the new datastore. I need a scripts where I can specify the list of VMs in scope of migration, destination datastore in csv or txt file ( input) and script that runs one svMotion operation at a time( one VM at a time). Can anyone guide me with the script , I plan to keep my diskformat same as source.
Also I should able to test the result and verify if the migration has been successful using script.
The error message will appear in the console.
The script will not stop, should it?
You mean something like this?
# CSV layout
#
# VMname,Datastore
# vm1,datastore1
# vm2,datastore1
#
$report = Import-Csv -Path targets.csv -UseCulture | %{
$obj = New-Object PSOBject -Property @{
VM = $_.VMname
Datastore = $_.Datastore
Result = ''
}
Move-VM -VM $_.VMname -Datastore $_.Datastore -Confirm:$false
if((Get-VM -Name $_.VMname | Get-Datastore | select -ExpandProperty Name) -ne $_.Datastore){
$obj.Result = 'Failed'
}
else{
$obj.Result = 'Ok'
}
$obj
}
$report | Export-Csv -Path report-csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
# CSV layout
#
# VMname,Datastore
# vm1,datastore1
# vm2,datastore1
#
Import-Csv -Path targets.csv -UseCulture | %{
Move-VM -VM $_.VMname -Datastore $_.Datastore -Confirm:$false
if((Get-VM -Name $_.VMname | Get-Datastore | select -ExpandProperty Name) -ne $_.Datastore){
Write-Error -Message "svMotion of $($_.VMname) to $($_.Datastore) failed!"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for quick response !
Could you tell me me where will "Write-Error -Message "svMotion of $($_.VMname) to $($_.Datastore) failed!"" throw the error ? Is it possible to get the result output in csv or text file ?
The error message will appear in the console.
The script will not stop, should it?
You mean something like this?
# CSV layout
#
# VMname,Datastore
# vm1,datastore1
# vm2,datastore1
#
$report = Import-Csv -Path targets.csv -UseCulture | %{
$obj = New-Object PSOBject -Property @{
VM = $_.VMname
Datastore = $_.Datastore
Result = ''
}
Move-VM -VM $_.VMname -Datastore $_.Datastore -Confirm:$false
if((Get-VM -Name $_.VMname | Get-Datastore | select -ExpandProperty Name) -ne $_.Datastore){
$obj.Result = 'Failed'
}
else{
$obj.Result = 'Ok'
}
$obj
}
$report | Export-Csv -Path report-csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You nailed it. I was exactly looking for something like this !. Could you confirm if this script will move ONLY the VMs I specify on the input targets.CSV file and NOT all the VMs in my inventory.
Thanks in advance !!