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:\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 -Path D:\Temp\NSX-T\mycred.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:\Temp\NSX-T\mycred.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
}
You can use the Continue statement in the else-block to jump to the next iteration of the Foreach loop.
For example, if the VM is not found, the script will jump back to the Foreach and run with the next VM
$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"
Continue
}
# Rest of the script
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use the Continue statement in the else-block to jump to the next iteration of the Foreach loop.
For example, if the VM is not found, the script will jump back to the Foreach and run with the next VM
$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"
Continue
}
# Rest of the script
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Thank you so much for the correction. I have now corrected the entire script with error checks and it works perfect
With your help and advise now i am picking up on writing scripts. Thanks a million for helping out
##Here is the correct script which works perfect. Sharing it so anyone like me can use to for their environment
#Requires -Module VMware.VimAutomation.Core
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:\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 -Path D:\Temp\NSX-T\mycred.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:\Temp\NSX-T\mycred.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***
####################################################################################
####################################################################################
# Search the VM to be migrated and validate VM if found else skip to next record
# on the CSV
####################################################################################
Foreach ($VMdetail in $VMdetails) {
$VM = Get-VM -Name $VMdetail.VMNAME -ErrorAction SilentlyContinue
If ($VM) {
Write-Host "VM is a alive on the source VC..." -ForegroundColor Yellow $VM
}
Else {
Write-Host 'VM' $VMdetail.VMNAME' Cannot be found' -ForegroundColor Magenta
continue
}
####################################################################################
# Check if there is a Destination ESXi host on the specified cluster
# If found proceed or skip to next record
####################################################################################
$destination = Get-cluster $vmdetail.TgCluster -ErrorAction SilentlyContinue |Get-VMHost |Select-Object -First 1
if ($destination){
Write-Host "VM will be placed on following ESXi host..." -ForegroundColor Yellow $destination
}
Else {
Write-host "Destination ESXi host" $vmdetail.TgCluster "is not Accessible" -ForegroundColor Magenta
continue
}
####################################################################################
# Check if the Network Adapter for the VM is found
# If found proceed or skip to next record
####################################################################################
$networkAdapter = Get-NetworkAdapter -vm $VM -ErrorAction SilentlyContinue
If ($networkAdapter){
Write-Host "VM Network Adaptor info..." -ForegroundColor Yellow $networkAdapter
}
Else {
Write-Host "Network Adpater cannot be attahced and migration will fail" -ForegroundColor Magenta
continue
}
####################################################################################
# Check if the destination vDS switch is available, also check if the destination
# PortGroup is found, if unavailable skip to the next record
####################################################################################
$destinationPortGroup = Get-VDSwitch -Name $VMdetail.TgSwitch -ErrorAction SilentlyContinue |Get-VDPortgroup -Name $VMdetail.TgPortGroup -ErrorAction SilentlyContinue
If ($destinationPortGroup){
Write-Host "VM will be moved to following PortGroup... " -ForegroundColor Yellow $destinationPortGroup
}
Else {
Write-Host "vDS Switch or PortGroup cannot be found " -ForegroundColor Magenta
continue
}
####################################################################################
# Check if the destination Datastore is available if found
# Proceed , if unavailable skip to the next record
####################################################################################
$destinationDatastore = Get-Datastore $VMdetail.TgDatastore -ErrorAction SilentlyContinue
if ($destinationDatastore) {
Write-Host "VM will be moved to following datastore... " -ForegroundColor Yellow $destinationDatastore
}
Else {
Write-Host " Destination datastore not found" -ForegroundColor Magenta
Continue
}
####################################################################################
# Move-VM does the actual move of the VM provided all of the above checks are
# Satisfied and completes the migration.
####################################################################################
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"
continue
}
# 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
####################################################################################
# *** End of Migration ***
####################################################################################
####################################################################################
# Display VM information after Migration
####################################################################################
Drawline
Get-VM $VMdetails.VMNAME -ErrorAction SilentlyContinue | 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
}