Newbie to powershell i am just doing search and modify the code little, I am trying to set a permission FULL Control to our shared users folder (per user per folder permission). I managed to add a new folder and new user but the permission is not checked in the folder assigned. Please assist Thank you
# Import the CSV file
$csvFilePath = "C:\\path\\folders.csv"
$folders = Import-Csv -Path $csvFilePath
# Loop through each row in the CSV
foreach ($folder in $folders) {
# Create the folder on the remote server
$folderPath = "\\ypdcwnwinfs001\\H$\\Home" + $folder.FolderName
New-Item -ItemType Directory -Path $folderPath -Force
# Set permissions for the folder
$acl = Get-Acl -Path $folderPath
$user = $folder.users
$permission = "FullControl" #,"Read","Write","Modify" # Can be Read, Write, Modify, FullControl
$accessType = "Allow"
# Create a new access rule
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, $permission, $accessType)
# Add the access rule to the folder's ACL
$acl.SetAccessRule($accessRule)
$acl.AddAccessRule($accessRule)
# Set the new ACL for the folder
Set-Acl -Path $folderPath -AclObject $acl
}
I am expecting to apply full control permission to users folder but i am getting error:
Exception calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
Newbie to powershell i am just doing search and modify the code little, I am trying to set a permission FULL Control to our shared users folder (per user per folder permission). I managed to add a new folder and new user but the permission is not checked in the folder assigned. Please assist Thank you
# Import the CSV file
$csvFilePath = "C:\\path\\folders.csv"
$folders = Import-Csv -Path $csvFilePath
# Loop through each row in the CSV
foreach ($folder in $folders) {
# Create the folder on the remote server
$folderPath = "\\ypdcwnwinfs001\\H$\\Home" + $folder.FolderName
New-Item -ItemType Directory -Path $folderPath -Force
# Set permissions for the folder
$acl = Get-Acl -Path $folderPath
$user = $folder.users
$permission = "FullControl" #,"Read","Write","Modify" # Can be Read, Write, Modify, FullControl
$accessType = "Allow"
# Create a new access rule
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, $permission, $accessType)
# Add the access rule to the folder's ACL
$acl.SetAccessRule($accessRule)
$acl.AddAccessRule($accessRule)
# Set the new ACL for the folder
Set-Acl -Path $folderPath -AclObject $acl
}
I am expecting to apply full control permission to users folder but i am getting error:
Share Improve this question edited Mar 23 at 23:18 Santiago Squarzon 61.7k5 gold badges24 silver badges54 bronze badges asked Mar 23 at 22:22 Ryan AlmadinRyan Almadin 12 bronze badges 8 | Show 3 more commentsException calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
1 Answer
Reset to default 0Regarding the exception raised Exception calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated.", without citing part of the content of the CSV file, it will not be possible to have a good analysis of the cause of the error.
# The post is not clear whether the "Users" attribute is just one user or whether it can be multiple users. This part of the code was placed to avoid the most general case, which would be multiple users.
$folder = @{
FolderName = 'homeuser'
Users = 'domain\user1', 'domain\user2'
}
# Initialize variable
$folderRoot = '\\ypdcwnwinfs001\H$\Home'
$folderPath = $folderRoot + '\' + $folder.FolderName
$acl = Get-Acl -LiteralPath ( New-Item -ItemType Directory -Path $folderPath -Force )
$permission = 'FullControl'
$accessType = 'Allow'
$inheritance = 'ContainerInherit', 'ObjectInherit'
$propagation = 'None'
# Create the object as an access rule for each user and add to the ACL
Foreach ( $user in $folder.Users ) {
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule( $user, $permission, $inheritance, $propagation, $accessType )
$acl.AddAccessRule( $accessRule )
}
# Disable inheritance without copying permission
$acl.SetAccessRuleProtection( $true, $false )
# Write the new ACLs
Set-Acl -Path $folderPath -AclObject $acl
I used an online translator. I apologize for not being fluent in the language.
$folder.users
? Are these a single user or multiple ? If you can add an example of how your CSV looks like it might give a hint on whats wrong – Santiago Squarzon Commented Mar 23 at 23:12users
columns of your .csv. Please share a few lines of the data (anonymise as needed without changing the structure substantially). – Grismar Commented Mar 23 at 23:20H$
inside"\\ypdcwnwinfs001\\H$\\Home"
does not result in expansion (string interpolation), as you can easily verify yourself (while it's conceptually better to`
-escape all$
chars. meant to be used literally inside"...."
, it is technically only necessary for those$
instances that are followed by character(s) that would be legal in a variable name; and, yes, if expansion isn't needed,'...'
quoting is preferable). In other words: your explanation is incorrect and amounts to a distraction. – mklement0 Commented Mar 24 at 3:12InheritanceFlags
andPropagationFlags
parameters in your AccessRule. See this answer and this one and this one too and.. – Theo Commented Mar 24 at 9:48