So, it has been quite a while now since my last blog, and i'm starting to go into the DT's. Finally, I have some release!
One of my glorious tasks I have in my current role is to migrate File servers due to lifecycle events, acquisitions, upgrades...what-nots. While doing this users have a tendancy of copying data to their local workstation, then moving it back. Welcome to maintained Ownership of files/folders. Part of my team's responsiblities is to manage NTFS permisions on our servers. When the Owner is set to anything other than "BUILTIN\Administrators" permissions abilities can be lost until the local group has been given ownership again.
One of my best friends in PoShing is Google. Now, search google for powershell set ntfs owner and you'll find blisteringly long scripts and confusing answers, even some that say you must use third-party cmdlets. Lets do this as simple as possible, natively in PoSh:
FUNCTION
param (
[string]$path,
[string]$owner
)
$children = Get-ChildItem $path -Recurse | % {$_.FullName} foreach ($child in $children) {
$acl = Get-Acl $child
$split = $owner.Split("\")
if ($acl.Owner -ne $owner) {
if ($owner -match "BUILTIN") { $objGroup = New-Object System.Security.Principal.NTAccount($split[1]) }
else { $objGroup = New-Object System.Security.Principal.NTAccount($split[0],$split[1]) }
$strSID = $objGroup.Translate([System.Security.Principal.SecurityIdentifier])
$acl.SetOwner($strSID)
}
Set-Acl -Path $child -AclObject $acl
}
}
STEP-BY-STEP
[string]$path,
[string]$owner
)
else { $objGroup = New-Object System.Security.Principal.NTAccount($split[0],$split[1]) }
$acl.SetOwner($strSID)
We now have the corrected settings stored in our object $acl. It’s time to write it back to our file/folder.
Bingo, Done!
It is important to know that this script should be run from the local workstation or sever to where the data resides. It can be run on a remote file store passing the path using its full UNC and the group name as “%COMPUTERNAME%\%GROUP”, but will be extremely slow.
USAGE
SetNTFSOwner "folder\or\file" "DOMAIN\owner"
This function can be piped to easily. If you wanted to set the owner on all subdirectories, but not the parent simply:
ALIASES
% = ForEach-Object