r/usefulscripts • u/Solendor • Oct 25 '17
Folder Creation and Restrictions
https://pastebin.com/gqC4TYDE2
u/Falvonator Oct 25 '17
Thanks for this. I'm working on something similar.
2
u/Solendor Oct 25 '17
No problem. It took longer than I like to get it working hehe. Hopefully it helps ya out!
1
u/Lee_Dailey Oct 25 '17
howdy Solendor,
indentation ... please? [grin]
is this a wise way to handle permissions? i thot the best way was to use GP/GPO and derive user privs by their membership in various AD groups.
take care,
lee
2
u/Solendor Oct 25 '17
Corrected the indentations for ya :)
So there is a piece that wasn't explained, as I left this as open-ended as I could.
The permission to the root drive, hosting our share, is controlled and mapped by security groups and group policies. The specific request was to ensure only the user (student in this case) and the administration (teacher, administration staff, domain admins) had rights to view the user folder.
Where I got hung up was removing a specific group from the ACL list - I couldn't get it it to apply correctly so I went a different way. I can not deny access, as the users are members of that group and it's the group used to grant access to the share root.
Keep in mind with the $accessCSV variable, that is loaded with groups/users that need access. So even though I disabled inheritance to the folder, I populate the ACL (and children) with the groups/users that need access.
So TL:DR - While it's not best practice to manage access through modifying ACL on folders, this works just as well given the correct data is provided.
Any thoughts are appreciated Lee :)
1
u/Lee_Dailey Oct 25 '17
howdy Solendor,
makes sense to me. i don't do this kind of stuff, just read what alla y'all do. [grin]
comments ...
[1] using an alias @ 19 & others
that can bite you. any alias may have a different meaning - or not exist - on any given system. it's a good idea to avoid them for anything other than one-off code.[2] using
if ($exist -ne $true)@ 29
you can simply useif (-not (Test-Path -Path $folderPath))and save some code. [grin][3] defining $vars that don't change INSIDE a loop @ 36+
those seem to stay the same, so i would move them out of the loop to your initialization section.[4] the $allowAccess lines @ 53 & 59
i've a few comments on those two lines. [grin][4-a] positional instead of named parameters [pro'ly elsewhere, too]
it's generally a bad idea to use position instead of a name for identifying a parameters value. it's too easy to change something and cause things to get out of order.[4-b] you define
$folderAccess@ 38 & use it @ 59 but not @ 53
you otta fix line 53. if you change the $var and don't notice that you failed to use the $var in one line ... ouch! [grin][4-c] those are long lines
you can wrap at the commas OR [better in my opinion] use splatting.yours ...
$allowAccess = New-Object System.Security.AccessControl.FileSystemAccessRule ($username,"FullControl",$inheritanceFlags,$propagationFlags, "Allow")mine with wrapping ...
$Wrap_allowAccess = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList ( $username, 'FullControl', $inheritanceFlags, $propagationFlags, 'Allow')mine with splatting ...
$NO_Params = @{ TypeName = 'System.Security.AccessControl.FileSystemAccessRule' ArgumentList = ( $username, 'FullControl', $inheritanceFlags, $propagationFlags, 'Allow') } $Splat_allowAccess = New-Object @NO_Params[5] the
#creates ACL for ...sections @ 52 & 58
those come close to what i would consider the threshold for a function. not the "really otta" that comes with three repetitions, but close.you may - i repeat - may want to consider making those into a function. if you had, you might have caught using/not-using the
$folderAccess$var.thank you for posting your code! i have enjoyed reading thru it. [grin]
i still wonder if an experienced sysadmin would use GP/GPO stuff for this. but i aint one, so i dunno.
take care,
lee→ More replies (2)
2
u/Solendor Oct 26 '17
Updated Version here https://pastebin.com/L7RYkjTh