2 Replies Latest reply on Nov 14, 2017 11:57 AM by TheVMinator

    Nested try / catch bocks

    TheVMinator Master

      I have a try catch block that I'm using to storage vmotion a VM.  Can I put a second try catch block nested inside it when I look up the VM name?  For example:

       

      Import-Csv -Path 'vmlist.csv' -UseCulture |

      try{

      foreach {

      get-vm | $_.vmname

      move-vm -datastore $_.destinationdatastore

       

      #A whole bunch more code is here

      }

      catch

      {

      write-host "Vm couldn't be moved"

      return

      }

      }

       

      When I try to look up the Vm based on the name in my input file, sometimes it errors out ungracefully when the VM has been renamed or deleted.

      Will PowerCLI handle something like this?

       

       

       

      Import-Csv -Path 'vmlist.csv' -UseCulture |

      try{

      foreach {

      try{

      get-vm | $_.vmname

      }

      catch{

      write-host "This VM is no longer present in vCenter inventory under this name"
      }

      move-vm -datastore $_.destinationdatastore

      #A whole bunch more code is here

      }

      catch

      {

      write-host "Vm couldn't be moved"

      return

      }

      }

       

      I need to give a more graceful response when a VM doesn't exist but

      Can this be done and if so SHOULD it be done?

       

      Thanks!