Set File Permissions Recursively – Running on File Server

What problems does this solve?

This error: “Failed to enumerate objects in the container. Access is denied.”

# Set variables here
$directories='\\fileserver\users$\username$'
$identity='domain\username'
$access="Full"
$errorLogPath=((Get-Item -Path '.\').FullName+"\filePermissionsScriptingErrors.txt")

function setNtfsPermissions{
    param(
        $directories,
        $identity,
        $access,
        $errorLogPath    
    )
    $erroractionpreference='stop'

    # Include the required NTFSSecurity library from the PowerShell Gallery
    if (!(Get-InstalledModule -Name NTFSSecurity -ea Ignore)) {
      try{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        Install-Module -Name NTFSSecurity -Force
      }catch{
        write-host $_
        return $false
      }
    }

    # Setting permissions
    $count=$directories.count;
    $i=0;
    try{
        foreach ($directory in $directories){
            $i++;
            "Setting permissions on directory $i of $count`: $directory...";
            Add-NTFSAccess -Path $directory -Account $identity -AccessRights $access -ErrorAction Stop;
            }
        write-host "Process completed." -foregroundcolor green
        return $true
        }
    catch{
        $errorMsg = (Get-Date -Format g)+": "+ $_.Exception.Message+".. " + $directory;
        Add-Content $errorLogPath $errorMsg;
        "We have encountered some errors and a log has been generated at this location '$errorLogPath'.";
        return $false;
        }
}

setNtfsPermissions $directories $identity $access $errorLogPath
# Previous script

# Set variables here
$directories="\\FILESERVER01\dept01$","\\FILESERVER01\dept02$"
$identity="KIMCONNECT\SysAdmins"
$access="Full"
$errorLogPath=((Get-Item -Path ".\").FullName+"\filePermissionsScriptingErrors.txt")

<# Permissions
AppendData
ChangePermissions
CreateDirectories
CreateFiles
Delete
DeleteSubdirectoriesAndFiles
ExecuteFile
FullControl
GenericAll
GenericExecute
GenericRead
GenericWrite
ListDirectory
Modify
None
Read
ReadAndExecute
ReadAttributes
ReadData
ReadExtendedAttributes
ReadPermissions
Synchronize
TakeOwnership
Traverse
Write
WriteAttributes
WriteData
WriteExtendedAttributes
#>

# Include the required NTFSSecurity library from the PowerShell Gallery
if (!(Get-InstalledModule -Name NTFSSecurity)) {Install-Module -Name NTFSSecurity -Force}

# Setting permissions
$count=$directories.count;
$i=0;
try{
    foreach ($directory in $directories){
        $i++;
        "Setting permissions on directory $i of $count`: $directory...";
        Add-NTFSAccess –Path $directory –Account $identity –AccessRights $access -ErrorAction Stop;
        }
    }
catch{
    $errorMsg = (Get-Date -Format g)+": "+ $_.Exception.Message+".. " + $directory;
    Add-Content $errorLogPath $errorMsg;
    "We have encountered some errors and a log has been generated at this location '$errorLogPath'.";
    Break;
    }
finally{
    "Process completed."
    }

Leave a Reply

Your email address will not be published. Required fields are marked *