Error handling in Powershell functions

I'm trying to modify a script I wrote, to be modular, while I learn more about tool making. I'm at the point where I'm trying to make it handle errors and don't know how to get the functions to break out. Here's what I've got:

[CmdletBinding()]
param(
    [string]$DPMServerName = 'server1'
)

Function Get-Libraries {
    Write-Verbose ("Getting list of libraries connected to {0}." -f $DPMServerName)
    Try {
       Set-Variable -Name libraries -Value (Get-DPMLibrary $DPMServerName -ErrorAction Stop | Where {$_.IsOffline -eq $False})
    }
    Catch [Microsoft.Internal.EnterpriseStorage.Dls.Utils.DlsException] {
        Write-Error ("Cannot connect to the DPM library. It appears that the servername is not valid. The specific error message is: {0}" -f $_.Exception.Message)
        Return $_.Exception.Message
    }
    Catch {
        Write-Error ("Unknown error getting library. The specific error message is: {0}" -f $_.Exception.Message)
        Return
    }

    Foreach ($library in $libraries) {
        Write-Verbose ("Starting fast inventory on {0}" -f $library)
        Start-DPMLibraryInventory -DPMLibrary $library -FastInventory -ErrorAction SilentlyContinue
    }

    $libraries
}

Function Update-TapeStatus ($libs) { <### add parameter "$libs" to the function ###>
    Foreach ($library in $libs) {
        $tapes = Get-DPMTape -DPMLibrary $library | Where {$_.Location -notmatch "*slot*"} | Sort Location
        <### output the list of tapes ###>
        $tapes
    }
}

$liblist = Get-Libraries
If ($LASTEXITCODE) { 
    Write-Output $LASTEXITCODE
}
Else {
    Update-TapeStatus $liblist
}

If there is an error (e.g. getting the list of libraries), I want to present a custom message and stop the script. I'll want to do something similar for other functions. The way it is written though, I'm not getting the desired message.

How to do make this work? Thanks.

Answers


Based on your clarification, what is when an exception is thrown, PowerShell is trying to match the exception's type with what you specify in the catch clause.

I don't have any experience with the DPM module/snap-in, but I am wondering if the module actually loads the assembly containing the exception type you are trying to catch into the app domain. By the error you are getting, it does not appear to. Hopefully you know what the assembly name that contains this class is. Use the Add-Type cmdlet with the -AssemblyName parameter at the top of your script to load the assembly, so that the PowerShell runtime knows about the exception type you are looking for.

As a last note, it is probably not a good idea to use a class in a Microsoft.Internal namespace as it is not meant for use directly in code, but as support behind the scenes in teh .Net libraries. Microsoft could pull the rug out from under you with no warning.


Need Your Help

Is there a module equivalent of Python's argparse for node.js?

node.js module command-line-arguments

argparse for python makes it quick and easy to handle command-line input, handling positional arguments, optional arguments, flags, input validation and much more. I've started writing applications...

How to create my own function in the Entity in Symfony 2

php symfony symfony-2.3

I would like to override the Entities, Form ect.. in Symfony 2 to create my own functions. This to don't loose my code when i re-generate my Model from my database.