0
votes

I have CSV file where I have name and folder path. I want to provide file permission to each name and the folder path.

I am using the following code

$mt = Import-Csv “C:\Users\shaker\Desktop\access.csv” |
      ForEach-Object {Get-Acl -Path $_.file}

$Accessrule = Import-Csv “C:\Users\shaker\Desktop\access.csv” |
              ForEach-Object {New-Object System.Security.AccessControl.FileSystemAccessRule ($_.name,"FullControl","Allow")}
ForEach-Object {$mt.SetAccessRule($AccessRule)}

But I am getting some error

PS C:\Users\shaker> C:\Users\shaker\Desktop\f1.ps1
Cannot convert argument "rule", with value: "System.Object[]", for
"SetAccessRule" to type "System.Security.AccessControl.FileSystemAccessRule":
"Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"System.Security.AccessControl.FileSystemAccessRule"."
At C:\Users\shaker\Desktop\f1.ps1:6 char:17
+ ForEach-Object {$mt.SetAccessRule($AccessRule)}
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
1

1 Answers

0
votes

Your code essentially says "build a list of paths, build a list of access rules, then apply all access rules to all paths". What you probably want is apply an access rule for any given name to the corresponding path. You also need to write the ACL back if you want it to become effective.

Import-Csv "C:\Users\shaker\Desktop\access.csv" | ForEach-Object {
    $acl = Get-Acl $_.file
    $ace = New-Object Security.AccessControl.FileSystemAccessRule ($_.name, 'FullControl', 'Allow')
    $acl.SetAccessRule($AccessRule)
    $acl | Set-Acl $_.file
}