What my intention is if one of the check fails it should not got to the Move-VM it should display the appropriate error message and go to the next record on the CSV file
I have done that but it does not work, When everything is fione it works perfectly, if the portgroup or VM name or anything is incorrect it displays the correct error message also it throws red powercli errors and fails at Move-VM which is not very good. Can someone help to to add the right error trapping please
I have added the full script below and this is the part which i am having issues with
##################################################################################################
####################################################################################
# Kick off the vMotion VMs between Virtual Centers
####################################################################################
# Get the VM name to be migrated
##Command to Migrate VM ( includes compute and datastore live migration)
Foreach ($VMdetailin$VMdetails) {
$VM=Get-VM-Name$VMdetail.VMNAME
If ($VM){
Write-Host"VM is a alive on the source VC..."-ForegroundColorYellow $VM
}
Else {
Write"VM Cannot be found"
}
$destination=Get-cluster$vmdetail.TgCluster |Get-VMHost|Select-Object-First1
if ($destination){
Write-Host"VM will be placed on following ESXi host..."-ForegroundColorYellow$destination
}
Else {
Write"Destination ESXi host is not Accessible"
}
$networkAdapter=Get-NetworkAdapter-vm$VM
If ($networkAdapter){
Write-Host"VM Network Adaptor info..."-ForegroundColorYellow$networkAdapter
}
Else {
Write"Network Adpater cannot be attahced and migration will fail"
}
$destinationPortGroup=Get-VDSwitch-Name$VMdetail.TgSwitch |Get-VDPortgroup-Name$VMdetail.TgPortGroup
If ($destinationPortGroup){
Write-Host"VM will be moved to following PortGroup... "-ForegroundColorYellow$destinationPortGroup
}
Else {
Write"Network Adpater cannot be attahced and migration will fail"
}
$destinationDatastore=Get-Datastore$VMdetail.TgDatastore
Move-VM-vm$VM-Destination$destination-NetworkAdapter$networkAdapter-PortGroup$destinationPortGroup-Datastore$destinationDatastore|out-null -ErrorVariable$movevmerr
if ($movevmerr-eq$null ) {
Write-host" VM migration in progress ........."-ForegroundColorMagenta
}
Else {
Write-Host" Move-VM error $movevmerr"
}
# Write-Warning " VM cannote be moved due to configuration errors or heartbeat issue "
}
############################################################################################################
###Full Script###
Import-Module-NameVmware.powercli
####################################################################################
# Clear existing VC connections
####################################################################################
Clear-Host
try {
Disconnect-VIServer-Server$global:DefaultVIServers -Confirm:$false -Force-ErrorActionStop
Write-Warning-Message" All Virtual Center connections are Disconnected "
}
Catch {
Write-host "Administrator Message : There are no existing Virtual centre connection we are good to proceed "-ForegroundColorGreen
}
functionDrawline {
for($i=0; $i-lt (get-host).ui.rawui.buffersize.width; $i++) {write-host-nonewline-foregroundcolorcyan"-"}
}
####################################################################################
# Variables
####################################################################################
#$VCnames= Import-Csv -Path 'D:\Temp\NSX-T\VC.CSV' -UseCulture
$VCnames=Get-Content-Path'D:\Temp\NSX-T\VC.CSV'|Select-String'^[^#]'|ConvertFrom-Csv-UseCulture
# vCenter Source Details (SSO Domain Source UCP 4000)
$SrcvCenter=$VCnames.source
# vCenter Destination Details (SSO Domain Advisor )
$DstvCenter=$VCnames.destination
# vMotion Details
#$VMdetails = Import-csv -Path 'D:\Temp\NSX-T\Migration.CSV' -UseCulture
$VMdetails=Get-Content-Path'D:\Temp\NSX-T\Migration.CSV'|Select-String'^[^#]'|ConvertFrom-Csv-UseCulture
####################################################################################
####################################################################################
# Connect to source and destination Virtual centers
####################################################################################
# Connect to Source vCenter Servers
Connect-ViServer-Server$SrcvCenter-Credential ( Import-Clixml-PathD:\Temp\NSX-T\mycred.xml) -WarningActionIgnore|out-null
write-Host-foregroundcolorYellow"`nConnected to Source vCenter..."$SrcvCenter
# Connect to Destination vCenter Servers
Connect-ViServer-Server$DstvCenter-Credential ( Import-Clixml-PathD:\Temp\NSX-T\mycred.xml) -WarningActionIgnore|out-null
write-Host-foregroundcolorYellow"Connected to Destination vCenter..."$DstvCenter
Drawline
Write-Host"Processing ..................................."-ForegroundColorWhite
####################################################################################
####################################################################################
# Kick off the vMotion VMs between Virtual Centers
####################################################################################
# Get the VM name to be migrated
##Command to Migrate VM ( includes compute and datastore live migration)
Foreach ($VMdetailin$VMdetails) {
$VM=Get-VM-Name$VMdetail.VMNAME
If ($VM){
Write-Host"VM is a alive on the source VC..."-ForegroundColorYellow $VM
}
Else {
Write"VM Cannot be found"
}
$destination=Get-cluster$vmdetail.TgCluster |Get-VMHost|Select-Object-First1
if ($destination){
Write-Host"VM will be placed on following ESXi host..."-ForegroundColorYellow$destination
}
Else {
Write"Destination ESXi host is not Accessible"
}
$networkAdapter=Get-NetworkAdapter-vm$VM
If ($networkAdapter){
Write-Host"VM Network Adaptor info..."-ForegroundColorYellow$networkAdapter
}
Else {
Write"Network Adpater cannot be attahced and migration will fail"
}
$destinationPortGroup=Get-VDSwitch-Name$VMdetail.TgSwitch |Get-VDPortgroup-Name$VMdetail.TgPortGroup
If ($destinationPortGroup){
Write-Host"VM will be moved to following PortGroup... "-ForegroundColorYellow$destinationPortGroup
}
Else {
Write"Network Adpater cannot be attahced and migration will fail"
}
$destinationDatastore=Get-Datastore$VMdetail.TgDatastore
Move-VM-vm$VM-Destination$destination-NetworkAdapter$networkAdapter-PortGroup$destinationPortGroup-Datastore$destinationDatastore|out-null -ErrorVariable$movevmerr
if ($movevmerr-eq$null ) {
Write-host" VM migration in progress ........."-ForegroundColorMagenta
}
Else {
Write-Host" Move-VM error $movevmerr"
}
# Write-Warning " VM cannote be moved due to configuration errors or heartbeat issue "
}
#$vm | Move-VM -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore | out-null
####################################################################################
# Display VM information after Migration
####################################################################################
Drawline
Get-VM$VMdetails.VMNAME |Get-NetworkAdapter|
Select-Object @{N="VM Name";E={$_.Parent.Name}},
@{N="Cluster";E={Get-Cluster-VM$_.Parent}},
@{N="ESXi Host";E={Get-VMHost-VM$_.Parent}},
@{N="Datastore";E={Get-Datastore-VM$_.Parent}},
@{N="Network";E={$_.NetworkName}} |Format-List
Drawline
####################################################################################
# Disconnect all Virtual centers
####################################################################################
Write-Host" YOU WILL NOW BE DISCONNECTED FROM VIRTUAL CENTRE "-ForegroundColorMagenta
try {
Disconnect-VIServer-Server$global:DefaultVIServers -Confirm:$false -Force-ErrorActionStop
Write-Warning-Message" All Virtual Center connections are Disconnected "
}
Catch {
Write-host "Administrator Message : There are no existing Virtual centre connection we are good to proceed "-ForegroundColorGreen
}