在PowerShell中递归获取用户主目录

MDM*_*rra 3 powershell active-directory

所以,我正在深入研究 PowerShell。我的任务是重做域中每个主文件夹的权限(它们并不都属于同一个子目录 - 这太容易了)。我编写了一个批处理脚本,它采用两个参数:用户名和主文件夹路径,并通过 SetACL 将它们泵送。

我想使用 PowerShell 获取 OU 中每个用户的用户名和主文件夹。到目前为止,我可以获取用户名,但我不知道如何获取主目录。

到目前为止,这是我的 PowerShell(从网络上的各种来源借用):

$Dom = "LDAP://OU=Accounts,DC=myDomain,DC=local"
$Root = New-Object DirectoryServices.DirectoryEntry $Dom

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
$Selector.pagesize = 20000


# Basically this will only grab user accounts and not computer accounts.
$adobj= $selector.findall() | where {
    $_.properties.objectcategory -match "CN=Person*"
}
foreach ($person in $adobj) {
    $prop=$person.properties
    Write-host "$($prop.cn)"
}
Run Code Online (Sandbox Code Playgroud)

我最终会将 Write-host 行通过管道传输到 setACL 批处理文件中,但我现在只是编写输出以确保它是准确的。我试过添加$($prop.homeDirectory)到 Write-host 行,但没有运气。

任何指示或建议?

jsc*_*ott 6

Microsoft 更新了他们的 Active Directory powershell 模块,它包含在 RSAT 中。如果您不想使用第三方的模块,下面列出了“JustAnOrgUnit”OU 中所有用户的sAMAaccountNamehomeDirectory属性——与@nimizen 的回答几乎相同,只是没有 Quest 要求。

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=JustAnOrgUnit,DC=example,DC=com" -Filter * -Property * |
    Select-Object -Property sAMAccountName,homeDirectory |
        Export-CSV -Path C:\somefile.csv
Run Code Online (Sandbox Code Playgroud)