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\FolderOwners hip\ADUser .csv"
$DomainName = “Europe”
$ErrorActionPreference = "Continue"
foreach ($ADDetail in $ADDetails)
{
#SamAccountName,"HomeDirec tory"
$HomeDirectoryPath = $ADDetail.HomeDirectory
$ntAccount = $ADDetail.SamAccountName
Write-Verbose "$ntAccount : $HomeDirectoryPath"
$ntAccount = New-Object System.Security.Principal. NTAccount( "$DomainNa me","$ntAc count")
$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\$Folde rName" -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'
}
}
}
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\FolderOwners
$DomainName = “Europe”
$ErrorActionPreference = "Continue"
foreach ($ADDetail in $ADDetails)
{
#SamAccountName,"HomeDirec
$HomeDirectoryPath = $ADDetail.HomeDirectory
$ntAccount = $ADDetail.SamAccountName
Write-Verbose "$ntAccount : $HomeDirectoryPath"
$ntAccount = New-Object System.Security.Principal.
$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\$Folde
{
$_.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'
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.