简体   繁体   中英

Get user members of nested AD groups and their info

I have an AD group "Office-Users" in winch different departments user groups are member. Finance-dep holding all users in finance, and Finance-dep are member of Office-Users group and so on.

I like to have all users of the nested groups exported to csv with some more info per user.

I've got all the users out like this:

Get-ADGroupMember Office-Users | where{$_.ObjectClass -eq "Group"} | %{Write $_.Name;Get-ADGroupMember $_}

But it only returns name, SamAccountName, distinguishedName etc.

I miss the Description, lastLogonTimestamp, whenCreated, Enabled/Disabled for the users, so I can check who is using Office.

Is there a simple way to to this?

I already using this to export all users and their status/info, but it's not based on the Office group. I've tried to modify it, but I failed :)

$alist = "Name`tAccountName`tDescription`tLastLogonTimestamp`tCompany`twhenCreated`tAcctEnabled`tGroups`n"
$userlist = Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,lastLogonTimestamp,Company,whenCreated,Enabled,MemberOf | Sort-Object -Property Name
$userlist | ForEach-Object {
$grps = $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object
$arec = $_.Name,$_.SamAccountName,$_.Description,[datetime]::FromFileTime($_.lastLogonTimestamp).ToString('d MMMM yyyy'),$_.Company,$_.whenCreated,$_.Enabled
$aline = ($arec -join "`t") + "`t" + ($grps -join "`t") + "`n"
$alist += $aline
}
$alist | Out-File C:\temp\ADUsers.csv

/Kim

How about changing that first line to this?

Get-ADGroupMember Office-Users | where{$_.ObjectClass -eq "Group"} | %{Write $_.Name;Get-ADUser (Get-ADGroupMember $_) -properties *}

Now you have all properties for the users in those groups, and can selectively display whichever properties you care about.

Got this to work:

$alist = "Name`tAccountName`tDescription`tLastLogonTimestamp`tCompany`twhenCreated`tAcctEnabled`n"
$userlist = Get-ADGroupMember Office-Users -Recursive | Get-ADUser -properties * | Select-Object -Property Name,SamAccountName,Description,lastLogonTimestamp,Company,whenCreated,Enabled | Sort-Object -Property Name
$userlist | ForEach-Object {
$arec = $_.Name,$_.SamAccountName,$_.Description,[datetime]::FromFileTime($_.lastLogonTimestamp).ToString('HH:mm d MMMM yyyy'),$_.Company,$_.whenCreated,$_.Enabled
$aline = ($arec -join "`t") + "`t" + "`n"
$alist += $aline
}
$alist | Out-File C:\temp\Office-users.csv

Thank you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM