通过PowerShell将文件所有者设置为非受信任域中的用户

通过PowerShell将文件所有者设置为非受信任域中的用户,powershell,active-directory,acl,Powershell,Active Directory,Acl,我正在尝试将文件的所有者设置为来自另一个域的SID。 此域不受信任,因此以下操作将不起作用 PS > (Get-Acl .).SetOwner([System.Security.Principal.NTAccount]'TESTWORLD\barry') Exception calling "SetOwner" with "1" argument(s): "Some or all identity references could not be translated." At line:1

我正在尝试将文件的所有者设置为来自另一个域的SID。 此域不受信任,因此以下操作将不起作用

PS > (Get-Acl .).SetOwner([System.Security.Principal.NTAccount]'TESTWORLD\barry')
Exception calling "SetOwner" with "1" argument(s): "Some or all identity references could not be translated."
At line:1 char:1
+ (Get-Acl .).SetOwner([System.Security.Principal.NTAccount]'TESTWORLD\barry')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IdentityNotMappedException
我可以从其他域获取用户的SID,如下所示:

$c = Get-Credentials TESTWORLD.INVALID\AdminUser
$dc = Get-ADDomainController -Discover -DomainName TESTWORLD.INVALID -Service PrimaryDC | %{$_.HostName}
$sid = Get-ADUser -Filter {Name -eq "barry"} -Server $dc -Credential $c | %{$_.SID}

然后我想将此SID设置为该文件的所有者。这是怎么可能的?

如果允许远程计算机使用DCOM通信,请尝试以下操作。您需要将$Path设置为远程系统的本地路径。如果您使用的是备用凭据,请在$OptionalCred哈希表中提供凭据值:

试试这个:

$Path = "C:\Folder"
$OwnerSID = # SID string goes here #    
$Computer = $env:ComputerName
$OptionalCred = @{
    # Don't use this running against local machine
    #Credential = Get-Credential TESTWORLD.INVALID\AdminUser
}

$EscapedPath = [regex]::Escape($Path)
$FileSecuritySetting = Get-WmiObject Win32_LogicalFileSecuritySetting -Filter "Path='$EscapedPath'" -ComputerName $Computer @OptionalCred
$Win32SD = $FileSecuritySetting | Invoke-WmiMethod -Name GetSecurityDescriptor | select -ExpandProperty Descriptor

$NewOwner = ([wmiclass]"Win32_Trustee").PSBase.CreateInstance()
$NewOwner.SIDString = $OwnerSID
$Win32SD.Owner = $NewOwner

Invoke-WmiMethod -Path $FileSecuritySetting.__PATH -Name SetSecurityDescriptor -ArgumentList $Win32SD @OptionalCred

我想你做不到。我想不出任何好的理由来解释为什么安全系统会允许这样做,或者为什么你想这样做。@mjolinor-我正在从另一个域装载资源,比如:PS C:\>New PSDrive-Name TEST-PSProvider FileSystem-Root\\FILE01t.TESTWORLD.INVALID\vol01$-Credential$C。然后,我在TEST:\Filename处创建了一个文件,然后想将所有者从指定的凭据更改为TESTWORLD域中的另一个用户您是否尝试过重新写入SDDL字符串?我决不是SDDL字符串的主控程序,但当我尝试时,我遇到以下情况:使用1个参数调用SetSecurityDescriptorSddlForm时出现异常:SDDL字符串包含无效的sid或无法翻译的sid。能否使用该域的本机凭据在所述服务器上启动远程会话?