Hi Everyone
I need to create a script that will set NTFS permissions on files og folders.
Its important that the script only adds permissions.
I am struggeling a bit getting it to work. I have made many changes to get it to work, but i end up the same way each time.
This is what i have:
function setNFTSAccess {
$ShareFolder = Get-ChildItem D:\Shares\ -Recurse
foreach ($Folder in $ShareFolder) {
$Path = $Folder
\# specify permissions to be set
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("domain\username","Modify","Allow")
$acl.SetAccessRule($AccessRule)
\# set ACL
Set-Acl -path $Path -AclObject $acl
}
}
\# run functions
SetNFTSAccess
I get these errors:
You cannot call a method on a null-valued expression.
At D:\set_permissions.ps1:21 char:5
- $acl.SetAccessRule($AccessRule)
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (:) [], RuntimeException
- FullyQualifiedErrorId : InvokeMethodOnNull
Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At D:\set_permissions.ps1:25 char:24
- Set-Acl -AclObject $acl
- ~~~~
- CategoryInfo : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
You cannot call a method on a null-valued expression.
At D:\set_permissions.ps1:21 char:5
- $acl.SetAccessRule($AccessRule)
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (:) [], RuntimeException
- FullyQualifiedErrorId : InvokeMethodOnNull
Can any kind soul help me modify my script to get it working, it would be much appreciated.
Thanks in advance.