-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyPermissions.ps1
58 lines (45 loc) · 1.77 KB
/
ApplyPermissions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Config
$InputFile = "C:\Users\admin\Desktop\export.csv"
$CSV = Import-Csv -Path $InputFile -Delimiter ";"
# Get the list of user/group names
$AccountsName = @()
foreach ($property in ($CSV | Get-Member -MemberType NoteProperty)) {
if ($property.Name -ne "Chemin") {
$AccountsName += $property.Name
}
}
foreach ($Line in $CSV) {
$Path = $Line.Chemin
# Verify if that the expected permissions are present on that item
$NeedToChange = $false
foreach ($User in $AccountsName) {
$NewPermissionForUser = $Line | select -expand $User
if ($NewPermissionForUser -eq "") {
$NewPermissionForUser = $null
}
$CurrentPermissionForUser = (Get-NTFSAccess -Path $Path -Account $User).AccessRights
if ($NewPermissionForUser -ne $CurrentPermissionForUser) {
$NeedToChange = $true
break
}
}
# Verify that there isn't some unexpected permissions
# (a permission for a user that isn't present in the CSV InputFile)
foreach ($User in (Get-NTFSAccess -Path $Path).Account.AccountName) {
if ($UsersCSV.Contains($User) -eq $false) {
$NeedToChange = $true
break
}
}
# If the item isn't compliant with the expected permissions
# Clear all permissions and add the expected ones
if ($NeedToChange) {
Clear-NTFSAccess -Path $Path -DisableInheritance
foreach ($User in $AccountsName) {
$PermissionForUser = $Line | select -expand $User
if ($PermissionForUser -ne "") {
Add-NTFSAccess –Path $Path –Account $User -AccessRights $PermissionForUser -AppliesTo ThisFolderSubfoldersAndFiles
}
}
}
}