Reputation: 14745
I'm working on finishing my script, but I'm still missing an essential part. It all started with creating a function to import an Excel sheet and yesterday I've made the function to set the permissions. Now it's time to put them both to work, but that's where I'm struggling.
Excel sheet 'Permissions':
Folder | Employees | Plant managers | Plant workers
SHARE | R | C | L
SHARE\Folder 1 | C | R | R
SHARE\Folder 2 | L | R | L
When I import this Excel sheet, I have 2 different arrays
:
$Permissions | Format-Table
Folder: Employees: Plant managers: Plant workers:
SHARE R C L
SHARE\Folder 1 C R R
SHARE\Folder 2 L R L
$PermissionsReverse | Format-Table
Folder: SHARE: SHARE\Folder 1: SHARE\Folder 2:
Employees R C L
Plant managers C R R
Plant workers L R L
I need to find a way to combine them both so I can set the permissions on the correct folders for the correct security group.
The folders and the job descriptions are preceded with some text to generate the full path (UNC) and the SAMaccountName:
# SAMaccountName
My Prefix Employees
My Prefix Plant managers
My Prefix Plant workers
# Folder
\\Server1\Share
\\Server1\Share\Folder 1
\\Server1\Share\Folder 2
# R = ReadAndExecute, L = ListContents, C = Modify
I've already made a function called Set-Permissions
that applies permissions to a folder based on the switch ReadAndExecute, Change or ListContents
. In the end I want to be able to do something like this for each folder/SAMaccountName, but I don't know how to iterate through the arrays:
Set-Permissions -Folder "\\Server1\SHARE" -SAMaccountName "My Prefix Employees" -Grant ReadAndExecute
Set-Permissions -Folder "\\Server1\SHARE" -SAMaccountName "My Prefix Plant managers" -Grant Change
Set-Permissions -Folder "\\Server1\SHARE" -SAMaccountName "My Prefix Plant workers" -Grant ListContents
...
Thank you for your help.
Upvotes: 0
Views: 49
Reputation: 22861
Still not completely clear about your question. Below is a solution that will iterate the csv file and build the command for your "set-permissions" function.
function convert-permissions($perms) {
switch($perms) {
"R" { return "ReadAndExecute" }
"C" { return "Modify" }
"L" { return "ListContents" }
default { return "ListContents" }
}
}
$import = import-csv ".\path\to\your.csv"
$colnames = $($import | get-member | ? { $_.membertype -eq 'NoteProperty' }).name
$sharePrefix = "\\server\"
foreach ($row in $import) {
foreach ($p in $colnames) {
if ($p -ne 'Folder') {
write-host "Set-Permissions -Folder $sharePrefix$($row.folder) -SAMaccountName $p -Grant $(convert-permissions $row.$p)"
}
}
}
Remove the write-host
if the output looks correct. I still don't know how you're generating the "prefix" for your groups, so I've omitted. If it's just a static transformation, I suggest passing the names through another function and returning the modified name.
Upvotes: 1
Reputation: 174920
You can use the psobject
property to access object members: $_.psobject.Properties
# Generating an object akin to what OP describes
$customObject = ""|Select-Object @{N="Group1";E={"R"}},@{N="Group2";E={"C"}},@{N="Group3";E={"L"}},@{N="Folder";E={"S:\Share\Folder"}}
$customObject = $customObject |Select-Object @{N="MyPrefixGroup1";E={$_."Group1"}},@{N="MyPrefixGroup2";E={$_."Group2"}},@{N="MyPrefixGroup3";E={$_."Group3"}},*
# Get the noteproperties with "MyPrefix" prefixes
$permissions = $customObject.psobject.Properties |Where-Object {$_.MemberType -eq "NoteProperty" -and $_.Name -like "MyPrefix*"}
# Go through each one and set the permission
foreach($permission in $permissions)
{
# Remove "MyPrefix" from the NoteProperty name
$GroupName = $permission.Name-replace"MyPrefix",""
switch($permission.Value)
{
R { Set-Permissions $GroupName R; break }
C { Set-Permissions $GroupName C; break }
L { Set-Permissions $GroupName L; break }
}
}
Upvotes: 0