У меня есть функция Powershell, которая выполняет часть этого, но она не использует учетную запись домена, чтобы стать владельцем и добавить разрешения ... она использует локального администратора. Есть ли лучший способ сделать это в 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
}