Last active
December 1, 2015 21:23
-
-
Save WilliamBerryiii/6b022ea6533942e9497e to your computer and use it in GitHub Desktop.
Active Directory User & Groups Flatten Scan for Powershell V2.0 with output flattened to a single element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try { Import-Module ActiveDirectory -ErrorAction Stop } | |
Catch { Write-Host "Unable to load Active Directory module."; Break; } | |
$hostname = [system.environment]::MachineName | |
$users = Get-ADUser -Filter * -Server $hostname | |
$users = $users | % { | |
$un = Get-ADUser "$_" -Properties * -Server $hostname | |
$groups = Foreach ($group in $un.MemberOf){ | |
(Get-ADGroup $group -Server $hostname).Name | |
} | |
if($groups -ne $null){ | |
$groups = $groups | Sort | |
$groups = [string]::Join(",",($groups)) | |
} else { | |
$groups = [string]::Empty | |
} | |
$user = New-Object -typename PSObject | | |
Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $un.DistinguishedName -passthru | | |
Add-Member -MemberType NoteProperty -Name Enabled -Value $un.Enabled -passthru | | |
Add-Member -MemberType NoteProperty -Name SamAccountName -Value $un.SamAccountName -passthru | | |
Add-Member -MemberType NoteProperty -Name EmailAddress -Value $un.EmailAddress -passthru | | |
Add-Member -MemberType NoteProperty -Name whenChanged -Value (([DateTime]$un.whenChanged) -f 'F') -passthru | | |
Add-Member -MemberType NoteProperty -Name whenCreated -Value (([DateTime]$un.whenCreated) -f 'F') -passthru | | |
Add-Member -MemberType NoteProperty -Name Groups -Value $groups -passthru | |
return $user | |
} | |
$output = '' | Select name, raw | |
$output.name = 'AD Users & Groups' | |
$output.raw = '' | |
foreach($result in $users) { | |
$output.raw += $result | Select SamAccountName, DistinguishedName, Enabled, EmailAddress, whenCreated, whenChanged, Groups | |
$output.raw += "`r`n" | |
} | |
$output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment