Issue with Try and Catch error handling. I broke the script. how to use it

I was trying to add error handling to the script. So I added try and catch. however, it broke the script...See attached where I put try and catch. Below is a good script that is working without Try and catch. It makes a nice export of permissions. The script attached is where I added try and catch and it no longer works.

I would like to do error handling log for Directory and remote computer if they dont exist.

thank you so much

BELOW is WORKING script WITHOUT try and catch.

 Function Get-xACL {


        [Parameter(Mandatory = $True)]
        [Parameter(Mandatory = $True)]
        [Parameter(ValueFromPipelineByPropertyName = $True,
            ValueFromPipeline = $True)]
        [String[] $ComputerName = $env:COMPUTERNAME

    Begin {
        $Report = @()
        Clear-Variable ACLs -ErrorAction SilentlyContinue
        New-EventLog –LogName Application –Source “Get-xAcl” -ErrorAction SilentlyContinue

        $DumpAcls = {
            $DataToExport = @()
            $Dirs = @()

            Foreach ($d in $Directory) {
                if (Test-Path $d) {
                    $Dirs += $d -AS [System.IO.DirectoryInfo]
                    $Dirs += Get-ChildItem -Path $d -Recurse -Directory
                else {
                    # possible directory didn't exist.  Write message to application log
                    #Write-EventLog -LogName Application -Source "Get-xACL" -EventId 2202 -EntryType Error -Message "unable to find directory: $d"

            Foreach ($dir in $Dirs) {
                $Acls = (Get-Acl -Path $dir.FullName).Access

                Foreach ($acl in $Acls) {
                    If ($acl.IdentityReference.Value.ToString() -in $Group) {
                        $props = @{
                            "Path"              = $dir.FullName
                            "IdentityReference" = $acl.IdentityReference.Value.ToString()
                            "AccessControlType" = $acl.AccessControlType
                            "InheritanceFlags"  = $acl.InheritanceFlags
                            "PropagationFlags"  = $acl.PropagationFlags
                            "FileSystemRights"  = $acl.FileSystemRights
                            "IsInherited"       = $acl.IsInherited

                        $DataToExport += (New-Object psobject -Property $props)

                    }# end if

                }# end foreach

            }# end foreach

            return $DataToExport

        }# end DumpAcls

    }# end Begin

    Process {
        Foreach ($Computer in $ComputerName) {
            If ($Computer -ne $env:COMPUTERNAME) {
                Write-Verbose "Remote Computer - $Computer"
                $ACLs = Invoke-Command -ComputerName $Computer -ScriptBlock $DumpAcls -ArgumentList $Directory, $Group
            Else {
                Write-Verbose "Local computer - $Computer"
                $ACLs = Invoke-Command $DumpAcls -ArgumentList $Directory, $Group

            $Report += $ACLs

    End {
        $Report | Export-Csv "c:\temp\NTFSPermissions.csv" -NoTypeInformation  

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason CrawfordTransport NinjaCommented:
First off, nice script :)

Try Catch will only catch terminating errors, and even then in my opinion you have already included enough error handling with the if ($computer -ne $env:computername) statement and including it inside a try/catch is overkill unless you're trying to catch an error with Invoke-Command in which case you should probably include it inside the catch block by itself.  Try/Catch/Finally was a hard concept for me to fully grasp due to the terminating error condition.  Play around with it a little more with less moving parts...that's what I had to do.
As Jason wrote, you should pinpoint the command you want to catch instead of including many commands inside a try catch block.
Including lots of code does not allow you to intercept a given error.

Note: to intercept a non terminating error, add the switch
-Erroraction STOP 

Open in new window

to the command. This way, any error will be caught

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'm proposing my answer as best one as addresses original poster question. Jason Crawford's one does a good job in summarizing concepts in try/catch so it gets "assisted answer" mark.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.