Link to home
Start Free TrialLog in
Avatar of CaussyR
CaussyR

asked on

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'
                        }
                  }
}
Avatar of footech
footech
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial