New-Object script - need help

Welcome Forums General PowerShell Q&A New-Object script - need help

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 week, 5 days ago.

  • Author
    Posts
  • #128106

    Participant
    Points: 33
    Rank: Member

    HI!! This is my first post and I should have signed up ages ago. Newb here =)

     

    If I run this script, only the last object gets added (Senior Accountant) to the ACL. How can I fix this?

    $Acl = Get-Acl "C:\MyFolder"
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\HR Staff", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Budget Analyst", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\dbtest", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Volunteer Coord", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Admin Project Manager", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Database Programmer", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Finance Director", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\HR Manager", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Office Coordinator", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Payroll Accountant", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Accounting Manager", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Accounting Staff", "Modify","Allow")
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ("03NH\Senior Accountant", "Modify","Allow")
    $Acl.SetAccessRule($Ar)
    Set-Acl "C:\MyFolder" $Acl

     

     

    Thanks!!

  • #128115

    Participant
    Points: 85
    Rank: Member

    Every $Ar assignment overwrites the prior one. To make all these modification you need $Acl.SetAccessRule($Ar) after every $Ar assignment

  • #128119

    Participant
    Points: 33
    Rank: Member

    Sam you are the man!! And that makes a lot of sense now. Thank you.

  • #128130

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    A little different approach:

    $rules = @()
    
    $rules = [pscustomobject]@{
        Account = '03NH\HR Staff'
        Rights = 'Modify'
        ControlType = 'Allow'
    }
    
    $rules = [pscustomobject]@{
        Account = '03NH\Budget Analyst'
        Rights = 'Modify'
        ControlType = 'Allow'
    }
    
    $rules = [pscustomobject]@{
        Account = '03NH\dbtest'
        Rights = 'Modify'
        ControlType = 'Allow'
    }
    
    $Acl = Get-Acl "C:\MyFolder"
    
    foreach ($rule in $acls) {
        $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ($rule.Account,$rule.Rights,$rule.ControlType)
        $Acl.SetAccessRule($Ar)
    }
    
    Set-Acl "C:\MyFolder" $Acl
    

You must be logged in to reply to this topic.