【发布时间】:2020-06-18 14:45:37
【问题描述】:
在网上搜索,我发现了 2 个能够更改文件和文件夹所有者的脚本。对此进行测试时,它在 PowerShell 1.0 中完美运行。现在我正在尝试将两者结合起来,以便它们递归地工作,因为我们的文件夹中有超过 500 个子目录和文件。这是一项艰巨的工作..
我们希望:
- 在
\\server\C$\Folder上运行一个脚本(不使用外部工具) - 将所有文件和子文件夹的所有者更改为
BUILTIN\Administrators
问题:
- 每个脚本仅适用于 1 个文件或 1 个文件夹。如何将其组合在一个脚本中,以便将所有子文件夹和文件一起处理?可能将其放入 2 个不同的函数中并循环遍历它,或者..
Script1:将文件所有者更改为管理员
$File = "\\server\c$\Users\dir\Downloads\Target\TargetFile.txt"
$Account = New-Object System.Security.Principal.NTAccount("BUILTIN\Administrators")
$FileSecurity = new-object System.Security.AccessControl.FileSecurity
$FileSecurity.SetOwner($Account)
[System.IO.File]::SetAccessControl($File, $FileSecurity)
Script2:将文件夹所有者更改为管理员
$AdjustTokenPrivileges = @"
using System;
using System.Runtime.InteropServices;
public class TokenManipulator
{
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name,
ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public static bool AddPrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw ex;
}
}
public static bool RemovePrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_DISABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw ex;
}
}
}
"@
add-type $AdjustTokenPrivileges
$Folder = Get-Item "C:\Users\dir\Downloads\Target"
[void][TokenManipulator]::AddPrivilege("SeRestorePrivilege")
[void][TokenManipulator]::AddPrivilege("SeBackupPrivilege")
[void][TokenManipulator]::AddPrivilege("SeTakeOwnershipPrivilege")
$NewOwnerACL = New-Object System.Security.AccessControl.DirectorySecurity
$Admin = New-Object System.Security.Principal.NTAccount("BUILTIN\Administrators")
$NewOwnerACL.SetOwner($Admin)
$Folder.SetAccessControl($NewOwnerACL)
【问题讨论】:
-
我建议手动执行此操作。在 Windows 8 中,您可以进入文件夹属性、安全选项卡、高级按钮、“更改”所有者链接,提供新所有者并点击确定,选中“替换子容器和对象的所有者”复选框,点击应用。这比尝试使用 PowerShell 更快、更安全。
-
运行 cmd.exe 命令对我来说是一个更简单的解决方案。
takeown.exe /F $dest /R将我的当前用户设置为所有文件和子目录的所有者,icacls $dest /t /grant "Everyone:(F)"授予每个人的完全访问权限。$dest变量具有目标目录路径。 Powershell 以管理员身份运行。这适用于本地文件夹,例如:c:\myfolder,但我还没有测试它们的远程服务器路径\\server\C$\Folder
标签: powershell