我正在努力学习 PowerShell,但遇到了困难。
问题:我无法弄清楚如何正确地将用户属性导出到电子表格/csv。我也无法访问 RSAT 或 Get-ADUser。
最终目标:比较两个不同 Active Directory 域中同一用户的属性和数据格式。
当前代码:
$search = [ADSIsearcher] "(sAMAccountName=SampleUser*)"
$search.SearchRoot = [ADSI]"LDAP://Office.Domain.Example"
$search.FindOne().properties | Format-Table -Wrap #Exact what I want, but in file shape
#Exports attempted
$search.FindOne().properties | Export-CSV -NoTypeInformation "SampleUser.csv"
#Just gives properties of the attributes
New-Object PSObject -property $search.FindOne().properties | Export-CSV -NoTypeInformation "SampleUser.csv"
#I get the attribute names, but no values, only "ResultPropertyValueCollection", also its two rows, instead of two columns.
我绝对超出了我的能力范围,我认为我最大的知识差距是 PowerShell 对象如何工作。虽然我可以很好地制作表格,但将其解析为我可以轻松手动比较的格式是一件痛苦的事情。
有人能指出我的主要错误并指出我正确的方向吗?
其他上下文:我正在尝试构建一个 GUI 搜索工具来跨多个 AD 域查找员工。 Outlook 的地址簿速度非常慢,而且选项极其有限。此外,它不连接到其他域。例如:员工 ID 非常容易获得,但不能用于在地址簿中查找某人。
这可行,但我很确定代码执行的步骤超出了必要的步骤。
$search = [ADSIsearcher] "(sAMAccountName=SampleUser*)"
$search.SearchRoot = [ADSI]"LDAP://Office.Domain.Example"
$table = @{}
$user.Properties.GetEnumerator() | ForEach-Object {$table += @{$_.Key = $_.Value -join ';|;' }}
$table.GetEnumerator() | Export-Csv -NoTypeInformation "SampleUser.csv"