-
Notifications
You must be signed in to change notification settings - Fork 25
/
SetPermissions
32 lines (26 loc) · 1.04 KB
/
SetPermissions
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
#Requires -RunAsAdministrator
# create sample data
$array =@()
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Path' -Value 'C:\tmp'
$item | Add-Member -type NoteProperty -Name 'logins' -Value 'NT AUTHORITY\SYSTEM'
$item | Add-Member -type NoteProperty -Name 'permissions' -Value 'FullControl'
$item2 = New-Object PSObject
$item2 | Add-Member -type NoteProperty -Name 'Path' -Value 'C:\tmp'
$item2 | Add-Member -type NoteProperty -Name 'logins' -Value 'john'
$item2 | Add-Member -type NoteProperty -Name 'permissions' -Value 'Read'
$Array += $item
$Array += $item2
# read sample data
$array
# Create directory security object
$acl = New-Object System.Security.AccessControl.DirectorySecurity
$acl.SetAccessRuleProtection($true, $true)
# foreach in array
try{
foreach($login in $array){
$account = new-object System.Security.AccessControl.FileSystemAccessRule("$($login.logins)", "$($login.permissions)", "ContainerInherit, ObjectInherit", "None", "Allow")
$ACL.SetAccessRule($account)
$ACL | Set-ACL -path $login.Path
}
} catch {$_}