English 中文(简体)
2. 变更所有权撤销权
原标题:change ownership recursive folder powershell

我期待着将数千份档案和文件的所有权从一个特定用户改为另一个用户。

在文件夹结构内,大多数档案和文件夹都需要将其所有权从用户A改为用户B,但用户委员会拥有的一些文件需要留下来。

I had found a previous question which seemed to be asking the same as this, but I can no longer find it to reference. It did give the below though.

$FolderToScan = "G:FolderFolderFolder"
$OldOwner = "DomainUser"
$NewOwner = New-Object System.Security.Principal.NTAccount("Domain","User")

$files = Get-ChildItem -LiteralPath $FolderToScan -Recurse

Foreach ($file in $files)
{
    $f = Get-Item -LiteralPath $file.FullName
    $f = $f.GetAccessControl( Access )
    If ($f.Owner -eq $OldOwner) {
        $f.SetOwner($NewOwner)
        Set-Acl -path $file.FullName -aclObject $f
    }
}

When I run the above with the correct details in place of domain/user, nothing happens.

如果任何人能够把我引向正确的方向,那将是巨大的。

问题回答

when I tried the code it always returned nothing for for the owner field. try these changes in the foreach loop.

this worked for me

Foreach ($file in $files)
{
    Get-Item -LiteralPath $file.fullname
    $f = Get-Item -LiteralPath $file.FullName
    $acl = (get-acl $f)
    $acl.owner
    If ($acl.Owner -eq $OldOwner) {
        $acl.SetOwner($NewOwner)
        Set-Acl -path $file.FullName -aclObject $acl
    }
}

A simplified version can look like this:

$NewOwner = New-Object System.Security.Principal.NTAccount("Domain","User")
$OldOwner = "DomainUser"

Get-ChildItem -Recurse -Force "Path/To/Folder" | Get-Acl | ? { $_.Owner -eq "$OldOwner" } | % { $_.SetOwner($NewOwner); $_ } | Set-Acl

如果你不需要过滤器,你就只能放弃<代码>。 最低限值/代码>/<代码>?。





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签