1
1
I have a Powershell function that does part of this, but it doesn't use the domain account to take ownership and add permissions...it uses a local administrator. Is there a better way to do this in Powershell?
<#
.SYNOPSIS
Take ownership of a folder giving the ownership to ourdomain\myuser
.DESCRIPTION
Takes ownership of a file the way my boss said to do when deleting a user's home directory.
Using the GUI:
1. Right click the folder and select properties.
2. Click the "Security" tab.
3. Click the "Advanced" button.
4. Next to the "Owner:" label, click "Change"
5. Enter ourdomain\myuser
6. Click OK, OK, OK
7. Right click the folder and select properties.
8. Click the "Security" tab.
9. Click the "Advanced" button.
10. Click "Add"
11. Next to "Principal:" click "Select a principal"
12. Enter ourdomain\myuser
13. Click OK
14. Check "Full control"
15. Click OK, OK, OK
16. You should now be able to manipulate or delete the folder.
.NOTES
File Name : Microsoft.PowerShell_profile.ps1
.EXAMPLE
Take-Ownership R:\Redirected\Users\<username>
#>
function Take-Ownership {
param(
[String]$Folder
)
# Take ownership of the folder...
# (though I'd prefer if I could specify a user or group instead)
takeown.exe /A /F $Folder
# Obtain the current ACL for this folder.
$CurrentACL = Get-Acl $Folder
# Add FullControl permissions to the ACL for the user.
Write-Host ...Adding ourdomain\myuser to $Folder -Fore Yellow
$SystemACLPermission = "ourdomain\myuser","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$SystemAccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $SystemACLPermission
$CurrentACL.AddAccessRule($SystemAccessRule)
#Write-Host ...Adding Infrastructure Services to $Folder -Fore Yellow
#$AdminACLPermission = "ourdomain\myuser","FullControl","ContainerInherit,ObjectInherit"."None","Allow"
#$SystemAccessRule = new-object System.Security.AccessControl.FilesystemAccessRule $AdminACLPermission
#$CurrentACL.AddAccessRule($SystemAccessRule)
# Set the ACL again.
Set-Acl -Path $Folder -AclObject $CurrentACL
}
1Do I understand your question correctly, you want to take permission in another context than local Admin? If so, use more switches of takeown:
takeown /s system /u domain\username /p password /f $folder /a
more information intakeown /?
– SimonS – 2016-06-27T11:06:40.827