Powershell error handling

Posted on 2009-05-01
Last Modified: 2012-05-06
I have a script that lists the directories in a specific location, then goes through and sets the ACLs on them (using get-acl and set-acl). Basically, it takes the name of the folder, which happens to be the name of the user who owns the folder (Home Directories), and gives them modify permissions and sets the folder to inherit from the root folder.

However, when running the script, it errors out when it comes to a folder for which there is no matching user (in the case of terminated employees). I'm looking for a way to ignore that folder and simply move on. The "-ea silentlycontinue" parameter won't work unfortunately, as it's not actually a cmdlet that's erroring out. Attached is the code, and any help is appreciated!

I know I could write some code that would check if the user exists in AD before attempting to modify the ACL, but we're only talking about 3-4 out of ~2000, so that seems kind of like overkill at this point. If that's the best solution though, I'll go with that.

It errors out on the 3rd to last line, $ACLBase.SetAccessRule($AccessRule).

Set-Location d:\Data\Home

$Inherit = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"

$propogation = [System.Security.AccessControl.PropagationFlags]"None"

$Foldername = Get-ChildItem * | Where-Object {$_.attributes -match "Directory"}

foreach ($Fullpath in $Foldername) {

$ACLBase = Get-Acl d:\Data\Home\

#Uncomment the following line to verify default permissions during step debugging

#Set-Acl -Path $Fullpath -AclObject $ACLBase

$username = $Fullpath.Name

$AddACL = "advocatesinc\$username", "Modify", $Inherit, $propogation, "Allow"

$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $AddACL


$ACLBase | Set-Acl $Fullpath 


Open in new window

Question by:tilbard
    LVL 70

    Accepted Solution


    You might also consider checking for the user as below. It uses the Quest CmdLets but could be done natively if it's an appealing method.

    Otherwise... examples below.

    # Checking for a user
    $username = $Fullpath.Name
    # An "IsNull" type check
    If (Get-QADUser $username) {
      # Set the permissions
    # Just Ignore It...
    $ErrorActionPreference = "SilentlyContinue"
    $ErrorActionPreference = "Continue"
    # Catching the Exception - allowing it to be handled
    Set-Variable -Name ErrMsg -Value "OK" -Scope Script;
    Trap [Exception] {
      Write-Host "Error occurred, ignoring it"
      # Might set a variable so we can check for the error outside of Trap
      $Script:ErrMsg = ($_.Exception.Message.ToString()).Trim();
      # Or just write the message here
      Write-Host $Script:ErrMsg

    Open in new window

    LVL 1

    Author Closing Comment

    Thanks again, that's twice today now! Catching the exception with Trap worked, though just ignoring it by setting the ErrorActionPreference still errored out the script.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now