简体   繁体   中英

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM