PowerShell 3 - TRY & CATCH a Non-Termination Error

Posted on 2014-07-11
Last Modified: 2014-08-27
Hi everyone,

I have written a script that changes the ownership of homefolders and files to the AD user account.  There are some folders that are on the SAN that does not allow Access.  I have tried the TRY & CATCH error handling, but can not write the error to a file of the folder that has access denied error:

$ADDetails = Import-Csv "C:\PSScripts\FolderOwnership\ADUser.csv"
$DomainName =  “Europe”
$ErrorActionPreference = "Continue"

foreach ($ADDetail in $ADDetails)
      $HomeDirectoryPath = $ADDetail.HomeDirectory
      $ntAccount = $ADDetail.SamAccountName

            Write-Verbose "$ntAccount : $HomeDirectoryPath"
            $ntAccount = New-Object System.Security.Principal.NTAccount("$DomainName","$ntAccount")
            $SubDirectories = Get-ChildItem $HomeDirectoryPath -Recurse
                  Foreach ($FolderName in $SubDirectories)

                              #Set Parent Folder Ownership
                              Get-Item -LiteralPath "$HomeDirectoryPath" -ErrorAction SilentlyContinue | Get-Acl |ForEach-Object `
                                    Set-Acl -aclobject $_ -Path $_.PSPath
                                    $Path = Split-Path $_.Path -NoQualifier
                                    Write-Host $Path "Folder Owner Set -" $ntAccount
                                    Write-Host "Step 1"

                              #Set Child Item Ownership
                              Get-ChildItem -LiteralPath "$HomeDirectoryPath\$FolderName" -Recurse –ErrorAction SilentlyContinue | Get-Acl |ForEach-Object `
                                    Set-Acl -aclobject $_ -Path $_.PSPath
                                    $Path = Split-Path $_.Path -NoQualifier
                                    Write-Host $Path "File Owner Set -" $ntAccount
                                    Write-Host "Step 2"
                              Write-error -Message "c:\Temp\caught.txt"
                              # Out-File 'c:\temp\errors.txt'
Question by:CaussyR
    LVL 38

    Expert Comment

    For Try/Catch to catch the error it has to be terminating.  If the error is non-terminating you can make it terminating by using -ErrorAction parameter set to Stop for individual cmdlets, or you can set the $ErrorActionPreference variable to set it for everything.
    LVL 67

    Accepted Solution

    Absolutely agree. You won't get try/catch working with -EA SilentlyContinue.

    But you can always ask for the "success" internal variable $? after executing commands - it is $false if an error occured, and works with SilentlyContinue. $error is also still filled with the error messages (with index 0 being the most recent one).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shouldn't all users have the same email signature?

    You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now