Subscribe
Accepted Solution

try catch help

This is my first foray into trying to trap a specific error in powershell with try/catch and I am stuck.  I can't seem to figure out what exactly I need to use to trap this error.  I have a loop that is checking a collection of filers, this section of the code is looking for un-failed, un-owned disks.  Four of our filers have hardware ownership rather than software ownership so this command fails when run against them.  I want to trap for this specific error and just simply continue.  The code below is as far as I have gotten, I can't even trap the error and get it to write to the console.  What am I doing wrong?

I used this command to get the Sytem.InvalidOperationException; $error[0].Exception.psobject.typenames[0]

try          {Get-NaDiskOwner -Controller $controller | Where-Object {$_.Owner -lt '' -and $_.IsFailed -eq 0}}

  catch[System.InvalidOperationException]{"An Error Occurred"}

This is the error;

Get-NaDiskOwner : SANOWN not enabled.

At C:\Users\M00627~1.MFA\AppData\Local\Temp\Untitled5.ps1:4 char:16

+ Get-NaDiskOwner <<<<  -Controller $controller | Where-Object {$_.Owner -lt '' -and $_.IsFailed -eq 0}}

    + CategoryInfo          : InvalidOperation: (rchnas02n1:NaController) [Get-NaDiskOwner], ECIFSSHARINGVIOLATION

    + FullyQualifiedErrorId : ApiException,DataONTAP.PowerShell.SDK.Cmdlets.Disk.GetNaDiskOwner

Re: try catch help

Try this:

Get-NaDiskOwner -Controller $controller -ErrorAction SilentlyContinue

The Toolkit throws mostly non-terminating errors, so try/catch isn't going to help unless you set the error action to "Stop".

Re: try catch help

Ok, that explains why I couldn't get try/catch to work.  I will most likely use this syntax and then check the variable $err for anything other than the SANOWN error.  Thanks Clinton.

Get-NaDiskOwner -Controller $controller -ErrorAction SilentlyContinue -ErrorVariable err | Where-Object {$_.Owner -lt '' -and $_.IsFailed -eq 0}