简体   繁体   English

Powershell-在Active Directory中查找文件所有者

[英]Powershell - Looking Up File Owner in Active Directory

How do I look up for information about a file owner in Active Directory? 如何在Active Directory中查找有关文件所有者的信息? The following doesn't work because it parses Domain\\User to the Get-ADUser command 以下内容不起作用,因为它会将Domain \\ User解析为Get-ADUser命令

$owner = Get-Acl file.txt | Select-Object Owner | Out-String
Get-ADUser -Identity $owner

I would also like to be able to query users from other domains, as by default, Get-ADUser looks up on your local domain. 我还希望能够查询其他域中的用户,因为默认情况下,Get-ADUser会查询您的本地域。

The issue with Get-ADUser is that the it is looking for you to Get-ADUser的问题在于它正在寻找您

identify a user by its distinguished name (DN), GUID, security identifier (SID), Security Accounts Manager (SAM) account name or name. 通过用户的专有名称(DN),GUID,安全标识符(SID),安全帐户管理器(SAM)帐户名或名称来识别用户。

A string "domain\\username" does not fit into one of those expected. 字符串“ domain \\ username”不符合预期之一。 Instead how about something like this: 而是这样的事情:

$domains = @{
    DOMAIN = "dc.domain.local"
}
$file = "F:\temp\Re__.msg"
$fileOwner = Get-Acl $file | Select-Object -ExpandProperty Owner
$account = New-Object -TypeName PSObject -Property @{
    domain = $fileOwner.Split("\")[0]
    username = $fileOwner.Split("\")[1]
}

If($domains.ContainsKey($user.domain)){
    $server = $domains[$user.domain]
    Get-ADUser -Server $server -Identity $user.username
} Else {
    Write-Warning "No matching server for the domain: $($user.domain)"
}

Create a hashtable of domains and dc's from those domains. 创建域的哈希表,并从这些域创建DC。 Then query the owner from a $file . 然后从$file查询所有者。 Split that owner into its domain and username. 将该所有者分为其域和用户名。 Then using the $user.domain find the matching domain controller to search for the user. 然后使用$user.domain查找匹配的域控制器以搜索用户。

You can also use the .Net Framework to grab the SID and pass it to Get-ADUser: 您还可以使用.Net Framework来获取SID并将其传递给Get-ADUser:

$user = New-Object System.Security.Principal.NTAccount (get-acl 'file.txt').owner
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier]).Value
Get-ADUser $sid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM