PowerShell 3 - TRY & CATCH a Non-Termination Error

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)
{
      #SamAccountName,"HomeDirectory"
      $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)
                  {

                        Try
                        {
                              #Set Parent Folder Ownership
                              Get-Item -LiteralPath "$HomeDirectoryPath" -ErrorAction SilentlyContinue | Get-Acl |ForEach-Object `
                              {
                                    $_.SetOwner($ntAccount)
                                    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 `
                              {
                                    $_.SetOwner($ntAccount)
                                    Set-Acl -aclobject $_ -Path $_.PSPath
                                    $Path = Split-Path $_.Path -NoQualifier
                                    Write-Host $Path "File Owner Set -" $ntAccount
                                    Write-Host "Step 2"
                              }
                        }
                        Catch
                        {
                              Write-error -Message "c:\Temp\caught.txt"
                              # Out-File 'c:\temp\errors.txt'
                        }
                  }
}
CaussyRAsked:
Who is Participating?
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.

footechCommented:
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.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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).
0

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
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.