如何更有效地编写此PowerShell查询?

时间:2017-10-11 02:35:18

标签: powershell

我希望看到写一个班轮的清洁方式是什么。

Get-AdGroup -Filter * -Properties Name,Description,whenCreated,whenChanged,ObjectClass,GroupCategory,GroupScope,SamAccountName,DistinguishedName |
    Sort-Object Name |
    Select-Object Name,Description,whenCreated,whenChanged,ObjectClass,GroupCategory,GroupScope,SamAccountName,DistinguishedName |
    Select *,@{Name="Members";Expression={Get-ADGroupMember $_.Name | %{$_.SamAccountName+';'}}} |
    Export-Csv -Path .\Group.csv -NoTypeInformation

1 个答案:

答案 0 :(得分:1)

并将属性名称分配给变量,以便将它们全部写出两次,并将Select-Objectselect组合在一起:

$properties = "Name,Description,whenCreated,whenChanged,ObjectClass,GroupCategory,GroupScope,SamAccountName,DistinguishedName";
Get-AdGroup -filter * -properties $properties |
    Select-Object $properties,@{Name="Members";Expression={Get-ADGroupMember $_.Name | %{$_.SamAccountName+';'}}} |
    Sort-Object Name |
    Export-Csv -Path .\Group.csv -NoTypeInformation

注意:这是一个单行命令,但为了便于阅读,我将其分开。