Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

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'
                        }
                  }
}
0
CaussyR
Asked:
CaussyR
1 Solution
 
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
 
QlemoC++ DeveloperCommented:
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

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now