Set File Permissions Recursively – Running on File Server

# 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 *