powershell - NTFS permissions

Kenneth D 1 Reputation point
2020-08-26T13:34:47.93+00:00

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.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,831 Reputation points
    2020-08-26T15:04:45.707+00:00

    You might want to try using the File System Security PowerShell Module 1abd77a5-9c0b-4a2b-acef-90dbb2b84e85.

    It makes messing around with NTFS ACLs a lot easier and you'll be less likely to screw up your file system.

    The reason you got the error is that you never set the variable $acl.

    0 comments No comments