Announcement

Collapse
No announcement yet.

Script to change folder ownership

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to change folder ownership

    Hi Guys,

    I've been tasked with creating a script that will change the ownership of a folder to that of its folder name (which so happens to be the students name) for disk quota purposes.

    I currently have:
    Code:
    $rootfolder = Get-ChildItem -Path \\testsrv-file\staff\pupils
    foreach ($userfolder in $rootfolder) 
    {
            $userfolder.FullName
            If (get-aduser $userfolder) 
    	{
                Get-Acl $userfolder.FullName | Format-List
                $acl = Get-Acl $userfolder.FullName
                $acl.SetAccessRuleProtection($True, $False)
                $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
                $acl.RemoveAccessRuleAll($rule)
                $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
                $acl.AddAccessRule($rule)
                $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($userfolder.Name,"Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
                $acl.AddAccessRule($rule)
                $acct=New-Object System.Security.Principal.NTAccount("contoso",$userfolder.name)
                $acl.SetOwner($acct)
                Set-Acl $userfolder.FullName $acl
                Get-Acl $userfolder.FullName  | Format-List
            }
    }

    I'm quite new to Powershell so not too sure whats going wrong but I'm getting the following error message on my test server:
    Code:
    PS C:\users\a.todd\desktop> c:\users\a.todd\desktop\newest.ps1 
    
    \\testsrv-file\staff\pupils\pupil1
    
    Get-ADUser : Cannot bind parameter 'Identity'. 
       Cannot convert the "pupil1" value of type "System.IO.DirectoryInfo"
       to type "Microsoft.ActiveDirectory.Management.ADUser".
    
    At C:\users\a.todd\desktop\newest.ps1:5 char:23
    +         If (get-aduser <<<<  $userfolder)
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADUser
    Any help would be greatly appreciated.

    Andrew
    Last edited by Rems; 6th November 2012, 22:36.

  • #2
    Re: Script to change folder ownership

    Try get-ADUser -Identity $userfolder
    The $userFolder name, is that equal to the sAMAccountName from a certain user?

    You might check it by adding a write-host $userfolder in front of the if statement
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: Script to change folder ownership

      try this:

      If (get-aduser $userfolder.name)


      /Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Script to change folder ownership

        Thanks Guys I haven't had a chance to check this as we're migrating from VMware to HyperV so its been a bit hectic!

        Andrew

        Comment

        Working...
        X