-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ADUsersInOrganization.ps1
110 lines (85 loc) · 3.79 KB
/
Get-ADUsersInOrganization.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<#
.SYNOPSIS
.DESCRIPTION
This script will produce a CSV file containing the entire organization within a reporting chain under a top level person.
The CSV contains Active Directory user attributes for each person who report up to a specified top level person, pulling the data from Active Directory
.TESTING
.NOTES
Author : KyleGW
Version : 3.1
.MODIFICATIONS
.EXAMPLE
#>
$scriptShortName = "Org AD Users"
$filePath = $env:PUBLIC
$filename = "$((Get-Date).ToString("yyyy-MM-dd HHmmss")) - $env:COMPUTERNAME - $scriptShortName.csv"
$outputFile = New-Item -type file (Join-Path $filePath $filename)
$global:firsttime = $true
$global:numpeopleinorg = 0
$toplevelPerson = ""
# used for debugging with F8
# $searchstring = $toplevelPerson
Function Get-DirectReports
{
param ($searchstring)
try{$adrecord = (([adsisearcher]"$searchstring").FindOne())}catch{}
if ($adrecord -eq $null)
{
$adrecord = ([adsisearcher]"displayname=$searchstring").FindOne()
}
if($adrecord -eq $null)
{
$adrecord = ([adsisearcher]"name=$searchstring").FindOne()
}
if($adrecord -eq $null)
{
$adrecord = ([adsisearcher]"email=$searchstring").FindOne()
}
if($adrecord -ne $null)
{
$hasreports = $false
$numtotalreports = 0
$numdirectreports = 0
if ($adrecord.properties.directreports -ne $null){$hasreports = $true}
if($hasreports)
{
$numdirectreports = $adrecord.properties.directreports.count
$adrecord.properties.directreports | % {
$adsearchname = $_
#$searchstring = ([regex]::Split($adsearchname,'([^,]*,[^,]*)')[1]).replace("\","")
$searchstring = $adsearchname.split(",")[0]
Write-Host "Input [$($_)] : Searching for [$searchstring]"
Get-DirectReports $searchstring
}
}
#put desired data into a hashtable
$hash = [ordered]@{
EmployeeType = [string]$adrecord.properties.employeetype
Title = [string]$adrecord.properties. title
Name = [string]$adrecord.properties.cn
department = [string]$adrecord.properties.department
hasDirectReports = $($hasreports)
directreports = $numdirectreports
Manager = $(($adrecord.properties.manager).split(",")[0]) #need to add regex
createdate = [string]$adrecord.properties.whencreated
account = [string]$adrecord.properties.samaccountname
email = [string]$adrecord.properties.mail
emailForOutlook = "$($adrecord.properties.mail);"
} #end hash
#create a new powershell object out of the data so that we get all of the PS Object goodness
$Object = New-Object PSObject -Property $hash
#here we ouput on the fly instead of building an in-memory collection (large dataset processing)
#if first line, output header data
if($firsttime -ne $false){$Object | ConvertTo-CSV -OutVariable OutData -notype; $OutData[0..0] | ForEach-Object {Add-Content -Value $_ -Path $outputFile};$global:firsttime = $false }
#convert our object to CSV then output it to the output file without the header line
$Object | ConvertTo-CSV -OutVariable OutData -notype
$OutData[1..($OutData.count - 1)] | ForEach-Object {Add-Content -Value $_ -Path $outputFile}
$global:numpeopleinorg++
}
else{
Write-Host "Did not find $searchstring"
}
}
Get-DirectReports $toplevelPerson
Invoke-Item "$outputFile"
Write-Host "There are $global:numpeopleinorg people in the organization reporting up to $toplevelPerson"