合并 2 个脚本

Merging 2 scripts

我的任务是编制我们所有安全组的列表并获取有关这些组的特定信息,但我终究无法弄清楚如何将所有信息都放到一个 Csv 中。

这在从这些组中获取我需要的所有散装物品方面做得很好。

Get-ADGroup -Filter * |

where {$_.GroupCategory -eq "security"} |
Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"} |

Select DistinguishedName, GroupScope, Name, SamAccountName |

Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation

这个脚本是我尝试将所有信息与组成员数一起编译的尝试。

$Groups = Get-ADGroup -Filter * |

where {$_.GroupCategory -eq "security"} |
Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"}

ForEach ($Group in $Groups){

    (Get-ADGroup $Group.DistinguishedName -Properties *).member.count

    Select DistinguishedName, GroupScope, Name, SamAccountName |

    Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation -Append

    }

我还将在另一列中添加这些组的编辑日期,这样我们就可以看到尽管有成员,但哪些组确实不活跃。

为此,您需要在调用 Select-Object:

时使用 calculated property 表达式
Get-ADGroup -Filter * |
  Where {$_.GroupCategory -eq "security"} |
  Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
  Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
  Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"} |
  Select DistinguishedName, GroupScope, Name, SamAccountName, @{Name='MemberCount';Expression={(Get-ADGroup $_.DistinguishedName -Properties member).member.Count}} |
  Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation

这将为每个输入对象创建一个名为 MemberCount 的新 属性,并用成员计数

填充它