Unable to access mapped folder directory that was created with PowerShell script

Hello, I recently got help from EE creating a script that will configure NTFS permissions on a directory.

Here is the link to that previous topic, http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28687233.html.

Scenario
My script creates a new directory on the network share and then assigns NTFS Modify permissions for the end user.

Problem
The problem is that after the folder is created and NTFS Modify permissions assigned, the user is unable to access the mapped directory. When trying to access the mapped directory, the following message is seen:

Unable to access user directory mapped to letter 'I'
Work Around
After the script creates the directory and assigns NTFS Modify permissions to the end user it won't open. However, if I then go into the directory properties and 'toggle' the Modify check box (uncheck then check it back again) and hit Apply. Viola! The end user can now access the mapped directory.

Help
I need help figuring out how to get my script to work the first time with creating the directory and permissions to Modify for an end user and it not requiring me to go toggle the permissions. Why doesn't the directory recognize the location until I toggle the permissions?

function Set-Permission {
<#
.Synopsis
   Manage NTFS permissions on a directory and it's child items.
.DESCRIPTION
   Long description
.EXAMPLE
   Set-Permission -dirPath '\\my-server\user-share\joe.user' -samAccountName joe.user -accessLevel Modify -Verbose

   This will apply Modify permissions for samAccountName joe.user to a network share directory called 'joe.user' and it's child items
.EXAMPLE
   Another example of how to use this cmdlet
.INPUTS
   Inputs to this cmdlet (if any)
.OUTPUTS
   Output from this cmdlet (if any)
.NOTES
   General notes
.COMPONENT
   The component this cmdlet belongs to
.ROLE
   The role this cmdlet belongs to
.FUNCTIONALITY
   The functionality that best describes this cmdlet
#>

    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # Param1 help description
        [Parameter(Mandatory=$true,Position=0)]
        [Alias("Path")]
        [string]
        $dirPath,

        # Param2 help description
        [Parameter(Mandatory=$true,Position=1)]
        [string]
        $samAccountName,

        # Param3 help description
        [Parameter(Mandatory=$true, Position=2)]
        [ValidateSet("Modify")]
        [string]
        $accessLevel,
        
        # Param4 help description
        [Parameter(Mandatory=$false)]
        [string]
        $domain = 'My_Domain'
    )
    Begin {
        $domainAccount = $domain + "\" + $samAccountName
        $inheritanceFlags = "ContainerInherit, ObjectInherit"                     
        $propagationFlags = "None"                    
        $accessControlType = "Allow"

        $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
                        $domainAccount,$accessLevel,$inheritanceFlags,$propagationFlags,$accessControlType)
        
        Write-Verbose 'Print New Access Rule object'
        $AccessRule

        # Saving parent folder directory into variable
        $Parent = gi $dirPath | where {$_.psIsContainer -eq $true}
        $pFN = $Parent.FullName

        Write-Verbose 'Access Control List of parent folder before applying new rules'
        $pFN
        $oldacl = Get-Acl $Parent
        $oldacl |select accesstostring |fl
    } # \Begin
    Process {
        Write-Verbose "Applying $samAccountName $accessLevel permissions to parent $pFN"
        Add-Access -Path $Parent -Account $domainaccount -AccessRights $accesslevel 

        Get-ChildItem -path $dirPath -Recurse | 
            ForEach-Object { 
                $fnDir = $_.FullName
                Write-Verbose "Applying $samAccountName $accessLevel permissions to child $fnDir"
            }# \ForEach
        Get-ChildItem -path $dirPath -Recurse | Get-NTFSAccessInheritance | 
        Where-Object { -not $_.InheritanceEnabled } | Enable-NTFSAccessInheritance -PassThru
    } # \Process
    End {
        Write-Verbose 'New permissions have been set'
        get-childitem -Path $dirPath -Recurse | get-acl | out-gridview
    }# \End
} 

Open in new window

ryanmavesAsked:
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.

Robin CMSenior Security and Infrastructure EngineerCommented:
Honestly, just use icacls. PowerShell is great, but re-inventing the wheel as above is not worth the hassle. You could have moved on and done other more productive things. Let the PowerShell team at MS write a better way to do this for you in a future version :-)

...from a PowerShell guy who has spent too long trying to make up for the inadequacies of PowerShell vs the built-in Windows commands :-) Now I just take the path of least resistance - unless I'm trying to prove a point (usually to myself!) and have LOTS of spare time.
1
Steve BinkCommented:
First thing: robincm has the right of it.  The icacls utility is designed to do exactly this.  You could have been done already.

In other news, it looks like you're only applying the "Modify" permission.  You probably need to add "Read", as well as whatever permission allows directory traversal (slips my mind ATM).
0
ryanmavesAuthor Commented:
Well, I guess the verdict is to not use PS to manage permissions.

I played with the icacls and had some success. I was able to create a script using PS around the icacls for anyone interested in going this route, also.

Thanks for the input EE.

# The below script will grant a user Modify rights (which includes every up to Modify, i.e. Read/Write) and also is (oi) Object inherent and (ci) Container inherent. Basically meaning all subfolders and files will apply the same Modify rights for the user.

$DirectoryPath = 'C:\myfolder\folder1'
$Domain = 'mydomain'
$samAccountName = 'user domain samAccountName'

icacls $DirectoryPath /grant "$Domain\${samAccountName}:(oi)(ci)(m)"
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
ryanmavesAuthor Commented:
My own comment is the actual solution because I already knew about icacls. My previous question (documented in this question) and also this question pertain to using PS to accomplish permissions.

My comment is the solution because I actually described a script that incorporates icacls within PS.
0
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.