在PowerShell中recursion获取用户主目录

所以,我正在深入PowerShell。 我已经被授权在域中的每个主文件夹上重做权限(它们并不都在相同的子目录下 – 这太容易了)。 我写了一个批处理脚本,它包含两个参数:用户名和主文件夹path,并通过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)" } 

我最终要将Write-host行写入setACLbatch file,但是现在我只是编写输出以确保它是正确的。 我已经尝试将$($prop.homeDirectory)添加到写主机行,没有运气。

任何指针或build议?

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 

使用Quest的AD cmdlet,它们是免费的,真正简化了这种事情。

您可以从http://www.quest.com/powershell/activeroles-server.aspx获取它们

一旦你有这些加载,请尝试下面的脚本,但也读了Get-QADUser cmdlet。

 $csvfile = "C:\somefile.csv" $root = "OU=Accounts,DC=myDomain,DC=local" get-qaduser -SearchRoot $root ` -ObjectAttributes @{homeDirectory='*'} -IncludeAllProperties | ` Select-Object LogonName,HomeDirectory | ` Export-Csv $csvfile