sanshis
Enthusiast
Enthusiast

How to manage Faults with Powershell SDK.

Hi All

I want to migrate VMs from 1 host to other and i am doing it well in normal circumstances ..
but i want to manage all the faults that are available with MigrateVM_task.. I am also ok with Move-VM cmd let; if i can manage faults by that.

i know i can manage it through Tasks Status that are returned by MigrateVM_tasks but i want to know if there is any other way to do it like using try catch block or other method like $Error variable of poershell..

i want to take care of following Faults

MigrationFault --> MismatchedNetworkPolicies, MismatchedVMotionNetworkNames

Timedout --> This one is most important.

VmConfigFault -->CannotAccessVmDevice; CannotAccessNetwork  CannotAccessVmDevice


please guide me ahead.

--

Kind Regards

Sanshis.

0 Kudos
3 Replies
LucD
Leadership
Leadership

The Try-Catch construct is ideal for working with terminating exceptions.

Not sure what exactly you want to know, but the PowerCLI cmdlet itself will handle those faults you mentioned.

It depend if the cmdlet translates the fault into a terminating error or not.

If you want to catch non-terminating errors, your script will have to examine the $error variable.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sanshis
Enthusiast
Enthusiast

ok let me clear what i want to do;

i want to catch all the most possible terminating exception and thn based on errors want to resolve them in the same script and thn move that vm again; this i can say will be a fully automated task.


i need some help to differentiate exceptions; say

try {

move-vm -vm $vmname -Destination $Host

}

catch {

}

how catch block gonna be for different cases?? I saw your Handle Faults Carefully but instead of showing errors i want to resolve them based on what error i got; hope this will clear my point.

--

Sanshis

0 Kudos
LucD
Leadership
Leadership

Your code should take into account that there are terminating and non-terminating errors

You can do a combination in the code, something along these lines perhaps ?

Try {

     cmdlet ... -ErrorAction SilentlyContinue

}

Catch [exception1] {

# Handle terminiating fault 1

}

Catch [exception 2]{

# Handle terminiating fault 1

}

Finally {

# Check if any non-terminating errors in $error[0]

# Note that Finally is always executed !

}

The trick is of course to know which cmdlet can produce which type of exceptions.

Afaik, there is, besides possibly the cmdlet help, no easy way to find the possible exceptions.

You can do a general exception Catch and then check the exception type in a Switch statement to execute the correct code.

And the 'default' case will capture all other exceptions.

Something like this

Try {

     cmdlet ... -ErrorAction SilentlyContinue

}

Catch [System.Exception] {

     switch($_.Exception.GetType().FullName) {

        'Exception1'  { # Handle exception1}

        'Exception2'  { # Handle exception2}

         default {# Handle all others}

     }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference