暂时拥有文件夹的所有权

temporarily take ownership of a folder

我需要暂时取得配置文件文件夹的所有权,以便向安全组成员添加权限(只读)。我想知道如何在 powershell 中实现这一点。

我以前用过TakeOwn.exe,但由于它无法将所有权归还给原始所有者,所以我不能将其用于此目的。

我尝试使用一个名为 PowerShellAccessControl 的模块,我在 technet.

上找到了它
Import-Module $PSScriptRoot\modules\PowerShellAccessControl\PowerShellAccessControl.psd1

$path = "$PSScriptRoot\profileFolders\profile"

$AddAceParams = @{
    Principal = "SecurityGroup"
    FolderRights = "Read"
}

Get-SecurityDescriptor $path -PacSDOption (New-PacCommandOption -BypassAclCheck) | ForEach-Object {
    $OriginalOwner = $_.Owner

    $_ | Set-Owner -PassThru -Apply | 
        Add-AccessControlEntry @AddAceParams -PassThru |
        Set-Owner -Principal $OriginalOwner -Apply 
}

但是这段代码只会导致以下错误:

New-PacCommandOption : The term 'New-PacCommandOption' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我不知道我是否应该继续学习这个模块,或者是否有更好的方法。

在处理文件系统权限时,我倾向于使用 NTFSSecurity 模块,它写得很好,到目前为止我已经很成功了。

$path = "C:\folder"
$OriginalPermissions = Get-NTFSOwner $path

Set-NTFSOwner -Path $path -Account $env:USERNAME
Add-NTFSAccess -Path $path -Account 'DOMAIN\SecurityGroup' -AccessRights Read
Set-NTFSOwner -Path $path -Account $OriginalPermissions.Owner

注意:您确实需要安装该模块,如果您使用的是现代版本的 Powershell,这很容易,因为您只需使用 Install-Module -Name NTFSSecurity。如果是旧版本,您需要手动下载并安装该模块。


编辑:

另一种选择是使用 Enable-Privileges 为您的帐户授予 Backup, Restore, and Security 的权限。

有了这些,您将能够编辑权限,而无需您自己的帐户对数据具有明确的权限。这些命令的使用包含在上面 link 的文档中。确保在启用它们后 Disable-Privileges,因为一直使用这些 运行 不是好的做法。