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 ($VMdetail in $VMdetails) {
$VM = Get-VM -Name $VMdetail.VMNAME
If ($VM){
Write-Host “VM is a alive on the source VC…” -ForegroundColor Yellow $VM
}
Else {
Write “VM Cannot be found”
}
$destination = Get-cluster $vmdetail.TgCluster |Get-VMHost |Select-Object -First 1
if ($destination){
Write-Host “VM will be placed on following ESXi host…” -ForegroundColor Yellow $destination
}
Else {
Write “Destination ESXi host is not Accessible”
}
$networkAdapter = Get-NetworkAdapter -vm $VM
If ($networkAdapter){
Write-Host “VM Network Adaptor info…” -ForegroundColor Yellow $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… “ -ForegroundColor Yellow $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 ………” -ForegroundColor Magenta
}
Else {
Write-Host ” Move-VM error $movevmerr“
}
# Write-Warning ” VM cannote be moved due to configuration errors or heartbeat issue “
}
############################################################################################################
###Full Script###
Import-Module -Name Vmware.powercli
####################################################################################
# Clear existing VC connections
####################################################################################
Clear-Host
try {
Disconnect-VIServer -Server $global:DefaultVIServers -Confirm:$false -Force -ErrorAction Stop
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 “ -ForegroundColor Green
}
function Drawline {
for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan “-“}
}
####################################################################################
# Variables
####################################################################################
#$VCnames= Import-Csv -Path ‘D:TempNSX-TVC.CSV’ -UseCulture
$VCnames= Get-Content -Path ‘D:TempNSX-TVC.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:TempNSX-TMigration.CSV’ -UseCulture
$VMdetails = Get-Content -Path ‘D:TempNSX-TMigration.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 -Path D:TempNSX-Tmycred.xml) -WarningAction Ignore | out-null
write-Host -foregroundcolor Yellow “`nConnected to Source vCenter…” $SrcvCenter
# Connect to Destination vCenter Servers
Connect-ViServer -Server $DstvCenter -Credential ( Import-Clixml -Path D:TempNSX-Tmycred.xml) -WarningAction Ignore | out-null
write-Host -foregroundcolor Yellow “Connected to Destination vCenter…” $DstvCenter
Drawline
Write-Host “Processing ……………………………..” -ForegroundColor White
####################################################################################
####################################################################################
# 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 ($VMdetail in $VMdetails) {
$VM = Get-VM -Name $VMdetail.VMNAME
If ($VM){
Write-Host “VM is a alive on the source VC…” -ForegroundColor Yellow $VM
}
Else {
Write “VM Cannot be found”
}
$destination = Get-cluster $vmdetail.TgCluster |Get-VMHost |Select-Object -First 1
if ($destination){
Write-Host “VM will be placed on following ESXi host…” -ForegroundColor Yellow $destination
}
Else {
Write “Destination ESXi host is not Accessible”
}
$networkAdapter = Get-NetworkAdapter -vm $VM
If ($networkAdapter){
Write-Host “VM Network Adaptor info…” -ForegroundColor Yellow $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… “ -ForegroundColor Yellow $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 ………” -ForegroundColor Magenta
}
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 “ -ForegroundColor Magenta
try {
Disconnect-VIServer -Server $global:DefaultVIServers -Confirm:$false -Force -ErrorAction Stop
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 “ -ForegroundColor Green
}