【问题标题】:Get-ADUser properties in specific order to export into CSV按特定顺序获取 ADUser 属性以导出到 CSV
【发布时间】:2019-09-25 19:26:28
【问题描述】:

这里是半新手。我正在尝试使用 Get-ADUser 以特定顺序显示用户信息。 Get-ADUser 属性列表的默认顺序似乎不符合我们的需求。到目前为止我有;

$Users = Get-ADUser -Filter * -SearchBase $treeview.SelectedNode.Name -Properties Name, CN, SamAccountName, DisplayName, sn, GivenName, Initials, `
OtherName, mail, EmailAddress, EmployeeNumber, Company, StreetAddress, POBox, City, State, PostalCode, Country, Department, HomePhone, `
telephoneNumber, OfficePhone, MobilePhone, Fax, info, physicalDeliveryOfficeName, Title, Office, EmployeeID, Description, DistinguishedName, `
CanonicalName, AccountExpirationDate, Created, Enabled, HomeDirectory, HomeDrive, HomePage, LastLogonDate, LockedOut, logonCount, Manager, `
Modified, msNPAllowDialin, ObjectClass, objectSid, PasswordLastSet, UserPrincipalName, whenChanged | 
Select-Object Name, CN, SamAccountName, `
DisplayName, sn, GivenName, Initials, OtherName, mail, EmailAddress, EmployeeNumber, Company, StreetAddress, POBox, City, State, PostalCode, `
Country, Department, HomePhone, telephoneNumber, OfficePhone, MobilePhone, Fax, info, physicalDeliveryOfficeName, Title, Office, EmployeeID, `
Description, DistinguishedName, CanonicalName, AccountExpirationDate, Created, Enabled, HomeDirectory, HomeDrive, HomePage, LastLogonDate, `
LockedOut, logonCount, Manager, Modified, msNPAllowDialin, ObjectClass, objectSid, PasswordLastSet, UserPrincipalName, whenChanged `
| Export-Csv -NoTypeInformation -Path $LogFile

虽然下面的代码运行得相当快(1758 位用户需要 4 秒),但这似乎有点冗长,我相信还有更有效的方法。我已经研究过哈希表,但即便如此,只要付出以上两倍的努力。

【问题讨论】:

  • 将您的-Properties Long, List, of, Stuff 更改为'-Properties *` 以获取所有道具。然后建立你的Select-Object 列表。我会使用一个数组来保存属性列表,但这不是必需的。
  • @Lee_Dailey 我不使用 '-Properties *' 的唯一原因,对于 1758 位用户来说,使用 * 时从 4 秒到 24 秒。
  • 啊!这就说得通了。 [grin] 所以...将道具添加到列表中,并在您的两个位置使用该列表。

标签: powershell hashtable export-to-csv


【解决方案1】:

如果我们要遵循Lee_Dailey's 的建议,您可以将要过滤的属性存储到数组变量中。然后将该变量传递给-Property/-Properties 参数。

$properties = "DisplayName","sn","GivenName","Initials","OtherName","mail","EmailAddress","EmployeeNumber","Company","StreetAddress","POBox","City","State","PostalCode","Country","Department","HomePhone","telephoneNumber","OfficePhone","MobilePhone","Fax","info","physicalDeliveryOfficeName","Title","Office","EmployeeID","Description","DistinguishedName","CanonicalName","AccountExpirationDate","Created","Enabled","HomeDirectory","HomeDrive","HomePage","LastLogonDate","LockedOut","logonCount","Manager","Modified","msNPAllowDialin","ObjectClass","objectSid","PasswordLastSet","UserPrincipalName","whenChanged"

$Users = Get-ADUser -Filter * -SearchBase $treeview.SelectedNode.Name -Properties $properties |
    Select-Object -Property $properties

【讨论】:

  • 谢谢@AdminOfThings。如果我重新格式化这些字段有什么不同吗?例如,我的代码仍然在原始帖子中列出,除了在 Select-Object 命令之后,我正在重新格式化 CanonicalName 以删除用户名,即...@{ Name = 'OU'; Expression = { $_.CanonicalName.Split('/')[0..($_.CanonicalName.Split('/').count - 2)] -join '/' } }
  • 您可以拥有一个没有自定义属性的基本列表,称为$properties。然后你可以在你的命令中添加额外的属性,比如get-aduser -filter * -properties ($properties + 'CanonicalName') | select $properties,@{n='OU';e={$_.CanonicalName.Split('/') # stuff}}
  • 另一种方法是将计算的属性包含到您的变量中,例如$properties = 'Name','EmployeeID','CanonicalName',@{n='OU';e={$_.CanonicalName.Split('/')}}。然后你可以在你的get-aduser 命令中去掉那个计算出来的属性 --> get-aduser -filter * -properties $properties[0..2] | select $properties
猜你喜欢
  • 1970-01-01
  • 2013-02-11
  • 2013-05-08
  • 2019-11-25
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多