Hello guys,
I need some help with a script, that should match the permissions for user accounts from an excel csv file (with delimeter) and mirror them. We have two columns, and the permissions that are assigned on the shared folder for the users in the first column need to be mirrored to the users in the second column. Could you please take a look at the code and let me know, what I have done wrong because I am lost at the moment :)
#region Variables
$FolderPath = "C:\Temp"
$ErrorActionPreference = "SilentlyContinue"
#endregion
#Read the users and store them
$Users = Import-Csv -Path "C:\Temp\Users.csv" -Delimiter ","
#Create a hash table with the values (Key = Arctic, Value = ASH)
$HashTable=@{}
foreach($row in $Users)
{
$HashTable[$row.Arctic]=$row.ASH
}
#Get folder structure
$Folders = Get-ChildItem -Directory -Path $FolderPath -Recurse -Force
#Traverse folder
Foreach ($Folder in $Folders) {
#Get Folder ACL
$FolderAcl = Get-Acl -Path $Folder.FullName
ForEach ($Access in $FolderAcl.Access) {
#Get the current user Identity
$DomainUser = $FolderAcl.Access.IdentityReference
#Get the index of the separator
$s = $DomainUser.IndexOf("\")
#Trim the user
$User = $DomainUser.Substring($s+1)
if ($HashTable.ContainsKey($User)) {
#Get the corrsponding ASH userfrom the hashtable
$ASHUser = $Hashtable.GetEnumerator() | where {$_.Key -eq $User }
$AccessControlType1 = $Access.AccessControlType
$IdentityReference1 = $Access.IdentityReference
$IsInherited1 = $Access.IsInherited
$InheritanceFlags1 = $Access.InheritanceFlags
$PropagationFlags1 = $Access.PropagationFlags
$NewACL = New-Object system.security.accesscontrol.filesystemaccessrule("$ASHUser","$AccessControlType1","$IdentityReference1","$IsInherited1","$InheritanceFlags1","$PropagationFlags1")
$FolderAcl.SetAccessRule($NewACL)
Set-acl -Path $Folder.FullName -AclObject $FolderAcl
}
}
}
When I run the script, I receive no error, but no permissions are mirrored as well