-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-ADObject.ps1
186 lines (157 loc) · 5.63 KB
/
Find-ADObject.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<#
.Synopsis
Name: Find-ADObject.ps1
A PowerShell function to search Active Directory for objects like Printers, Users, Groups and Computers. Supports wildcards as *
.DESCRIPTION
A function to search Active Directory for objects.
Category (Users, Computers, Groups and Printers) can be specified, or you can search all categories at the same time.
It also supports wildcard so you don't have to know more then some of the text you are looking for.
.EXAMPLE
Find-ADObject -Name *SearchText*
Find-ADObject -Name *SearchText* -Category Users
Category can be "Users", "Computers", "Groups" or "Printers"
.NOTES
Original release Date: 18.06.2020
Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/)
.LINK
https://github.com/flemmingss/
#>
function Find-ADObject
{
Param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$false)]
[ValidateSet("Computers", "Groups", "Printers", "Users")]
[string]$Category
)
$Selected_Category = $Category
if ($Selected_Category -eq "Computers")
{
[string]$Selected_Category = "computer"
}
if ($Selected_Category -eq "Groups")
{
[string]$Selected_Category = "group"
}
if ($Selected_Category -eq "Printers")
{
[string]$Selected_Category = "printQueue"
}
if ($Selected_Category -eq "Users")
{
[string]$Selected_Category = "user"
}
#If category not selected
if ($Selected_Category -ne "computer" -and $Selected_Category -ne "group" -and $Selected_Category -ne "printQueue" -and $Selected_Category -ne "user")
{
$Selected_Category_Array = @("computer", "group", "printQueue", "user")
ForEach ($Selected_Category in $Selected_Category_Array)
{
$ObjectCategory = "$Selected_Category" #Options: printQueue, user, computer, group
$strFilter = "(&(objectCategory=$ObjectCategory)(Name=$Name))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name"
foreach ($i in $colPropList)
{
$objSearcher.PropertiesToLoad.Add($i) | Out-Null
}
$colResults = $objSearcher.FindAll()
$outputresults = foreach ($objResult in $colResults)
{
$objItem = $objResult.Properties
$objItem.name
}
if ($Selected_Category -eq "computer")
{
$results_computer = $outputresults
}
if ($Selected_Category -eq "group")
{
$results_group = $outputresults
}
if ($Selected_Category -eq "printQueue")
{
$results_printQueue = $outputresults
}
if ($Selected_Category -eq "user")
{
$results_user = $outputresults
}
}
if ($results_computer -eq $null)
{
Write-Host "No matches in computers" -ForegroundColor Red
}
else
{
Write-Host "Matches in computers:" -ForegroundColor Green
$results_computer
}
if ($results_group -eq $null)
{
Write-Host "No matches in groups" -ForegroundColor Red
}
else
{
Write-Host "Matches in groups:" -ForegroundColor Green
$results_group
}
if ($results_printQueue -eq $null)
{
Write-Host "No matches in printers" -ForegroundColor Red
}
else
{
Write-Host "Matches in printers:" -ForegroundColor Green
$results_printQueue
}
if ($results_user -eq $null)
{
Write-Host "No matches in users" -ForegroundColor Red
}
else
{
Write-Host "Matches in users:" -ForegroundColor Green
$results_user
}
}
#if category selected
else
{
$ObjectCategory = "$Selected_Category" #Options: printQueue, user, computer, group
$strFilter = "(&(objectCategory=$ObjectCategory)(Name=$Name))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name"
foreach ($i in $colPropList)
{
$objSearcher.PropertiesToLoad.Add($i) | Out-Null
}
$colResults = $objSearcher.FindAll()
$outputresults = foreach ($objResult in $colResults)
{
$objItem = $objResult.Properties
$objItem.name
}
if ($outputresults -eq $null)
{
Write-Host "No matches in $Category" -ForegroundColor Red
}
else
{
Write-Host "Matches in $Category :" -ForegroundColor Green
$outputresults
}
}
}
#end of script