I have a powershell script to collect folders and permissions on one system and another script to create the folder structure and apply permissions on a different system in a highly restricted environment.
I can't use robocopy to the new system. I have to ftp, so I'm using a csv file to collect and apply the permissions. Folders only - the files are transferred over ftp after the folders and permissions are in place (H/T to people who helped me with earlier problem on the "apply permissions" script (Powershell problem with Set-ACL from imported csv)).
When I test the "apply permissions" script on my local laptop, the folders are created with the permissions as expected. When I run the script with the same csv file on the secure system, it creates the folder structure, but it applies every explicit permission in the spreadsheet to every listed folder. The only difference in the scripts is the root folder I set as the starting point. Both systems are Windows 10 and have the same version of powershell.
I have full NTFS permissions where I create the directories, but I don't have permissions to run scripts in the secure environment so I have to select all and use the "run selection" option. I did the same thing on my laptop to rule out any potentially different behavior from that.
Here's an example of the folder/acl spreadsheet:
FolderPath | IdentityReference | FileSystemRights | InheritanceFlags |
---|---|---|---|
Activities | AD\User-ACTIVITIES.T | Read | None |
Activities | AD\user.adm | FullControl | ContainerInherit, ObjectInherit |
Activities\BWOVC1 | AD\User-BWOVC1 | Modify | ContainerInherit, ObjectInherit |
Here is the script:
# Location Where your folders are to be created
$RootDir = "C:\Users\User1\Documents\ACL"
Set-Location "$RootDir"
# Import CSV file from location
$Folders = Import-Csv "$RootDir\ACL-File.csv"
# Create Folders from FolderPath column in csv; set ACL
ForEach ($Folder in $Folders)
{
if(Test-Path $RootDir\$Folder.FolderPath) {continue} #{Write-Verbose "Folder: $Path Already Exists"}
else{
New-Item $Folder.FolderPath -type directory
}
$IdentityReference = $Folder.IdentityReference
$FileSystemRights = $Folder.FileSystemRights
$InheritanceFlag = "ContainerInherit, ObjectInherit"
$PropagationFlag = "None"
$AccessControlType = "Allow"
#From stackoverflow response;
# Define the access rule(s) to add to the ACL
$New_ACR = New-Object Security.AccessControl.FileSystemAccessRule $IdentityReference, $FileSystemRights, $InheritanceFlag, $PropagationFlag, $AccessControlType
# Get Access Control List from directory
$ACL = Get-Acl -Path ($RootDir + "\" + $Folder.FolderPath)
# Add new rule to ACL
$ACL.AddAccessRule($New_ACR)
# Apply updated ACL to directory
Set-Acl -Path ($RootDir + "\" + $Folder.FolderPath) -AclObject $ACL
}
Any idea why I'm getting different behavior on the different systems?