-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathGet-DomainComputer.ps1
202 lines (163 loc) · 9.84 KB
/
Get-DomainComputer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Function Get-DomainComputer {
<#
.SYNOPSIS
The Get-DomainComputer function allows you to get information from an Active Directory Computer object using ADSI.
.DESCRIPTION
The Get-DomainComputer function allows you to get information from an Active Directory Computer object using ADSI.
You can specify: how many result you want to see, which credentials to use and/or which domain to query.
.PARAMETER ComputerName
Specifies the name(s) of the Computer(s) to query
.PARAMETER SizeLimit
Specifies the number of objects to output. Default is 100.
.PARAMETER DomainDN
Specifies the path of the Domain to query.
Examples: "FX.LAB"
"DC=FX,DC=LAB"
"Ldap://FX.LAB"
"Ldap://DC=FX,DC=LAB"
.PARAMETER Credential
Specifies the alternate credentials to use.
.EXAMPLE
Get-DomainComputer
This will show all the computers in the current domain
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation001"
This will query information for the computer Workstation001.
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation001","Workstation002"
This will query information for the computers Workstation001 and Workstation002.
.EXAMPLE
Get-Content -Path c:\WorkstationsList.txt | Get-DomainComputer
This will query information for all the workstations listed inside the WorkstationsList.txt file.
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -Verbose
This will query information for computers starting with 'Workstation0', but only show 10 results max.
The Verbose parameter allow you to track the progression of the script.
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -Verbose -DomainDN "DC=FX,DC=LAB" -Credential (Get-Credential -Credential FX\Administrator)
This will query information for computers starting with 'Workstation0' from the domain FX.LAB with the account FX\Administrator.
Only show 10 results max and the Verbose parameter allows you to track the progression of the script.
.NOTES
NAME: FUNCT-AD-COMPUTER-Get-DomainComputer.ps1
AUTHOR: Francois-Xavier CAT
DATE: 2013/10/26
WWW: www.lazywinadmin.com
TWITTER: @lazywinadmin
VERSION HISTORY:
1.0 2013.10.26
Initial Version
#>
[CmdletBinding()]
PARAM(
[Parameter(
ValueFromPipelineByPropertyName=$true,
ValueFromPipeline=$true)]
[Alias("Computer")]
[String[]]$ComputerName,
[Alias("ResultLimit","Limit")]
[int]$SizeLimit='100',
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias("Domain")]
[String]$DomainDN=$(([adsisearcher]"").Searchroot.path),
[Alias("RunAs")]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
)#PARAM
PROCESS{
IF ($ComputerName){
Write-Verbose -Message "One or more ComputerName specified"
FOREACH ($item in $ComputerName){
TRY{
# Building the basic search object with some parameters
Write-Verbose -Message "COMPUTERNAME: $item"
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectSearcher
$Searcher.Filter = "(&(objectCategory=Computer)(name=$item))"
$Searcher.SizeLimit = $SizeLimit
$Searcher.SearchRoot = $DomainDN
# Specify a different domain to query
IF ($PSBoundParameters['DomainDN']){
IF ($DomainDN -notlike "LDAP://*") {$DomainDN = "LDAP://$DomainDN"}#IF
Write-Verbose -Message "Different Domain specified: $DomainDN"
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['DomainDN'])
# Alternate Credentials
IF ($PSBoundParameters['Credential']) {
Write-Verbose -Message "Different Credential specified: $($Credential.UserName)"
$Domain = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password) -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectCred
$Searcher.SearchRoot = $Domain}#IF ($PSBoundParameters['Credential'])
# Querying the Active Directory
Write-Verbose -Message "Starting the ADSI Search..."
FOREACH ($Computer in $($Searcher.FindAll())){
Write-Verbose -Message "$($Computer.properties.name)"
New-Object -TypeName PSObject -ErrorAction 'Continue' -ErrorVariable ErrProcessNewObjectOutput -Property @{
"Name" = $($Computer.properties.name)
"DNShostName" = $($Computer.properties.dnshostname)
"Description" = $($Computer.properties.description)
"OperatingSystem"=$($Computer.Properties.operatingsystem)
"WhenCreated" = $($Computer.properties.whencreated)
"DistinguishedName" = $($Computer.properties.distinguishedname)}#New-Object
}#FOREACH $Computer
Write-Verbose -Message "ADSI Search completed"
}#TRY
CATCH{
Write-Warning -Message ('{0}: {1}' -f $item, $_.Exception.Message)
IF ($ErrProcessNewObjectSearcher){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the searcher object"}
IF ($ErrProcessNewObjectCred){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the alternate credential object"}
IF ($ErrProcessNewObjectOutput){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the output object"}
}#CATCH
}#FOREACH $item
}#IF $ComputerName
ELSE {
Write-Verbose -Message "No ComputerName specified"
TRY{
# Building the basic search object with some parameters
Write-Verbose -Message "List All object"
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectSearcherALL
$Searcher.Filter = "(objectCategory=Computer)"
$Searcher.SizeLimit = $SizeLimit
# Specify a different domain to query
IF ($PSBoundParameters['DomainDN']){
$DomainDN = "LDAP://$DomainDN"
Write-Verbose -Message "Different Domain specified: $DomainDN"
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['DomainDN'])
# Alternate Credentials
IF ($PSBoundParameters['Credential']) {
Write-Verbose -Message "Different Credential specified: $($Credential.UserName)"
$DomainDN = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN, $Credential.UserName,$Credential.GetNetworkCredential().password -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectCredALL
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['Credential'])
# Querying the Active Directory
Write-Verbose -Message "Starting the ADSI Search..."
FOREACH ($Computer in $($Searcher.FindAll())){
TRY{
Write-Verbose -Message "$($Computer.properties.name)"
New-Object -TypeName PSObject -ErrorAction 'Continue' -ErrorVariable ErrProcessNewObjectOutputALL -Property @{
"Name" = $($Computer.properties.name)
"DNShostName" = $($Computer.properties.dnshostname)
"Description" = $($Computer.properties.description)
"OperatingSystem"=$($Computer.Properties.operatingsystem)
"WhenCreated" = $($Computer.properties.whencreated)
"DistinguishedName" = $($Computer.properties.distinguishedname)}#New-Object
}#TRY
CATCH{
Write-Warning -Message ('{0}: {1}' -f $Computer, $_.Exception.Message)
IF ($ErrProcessNewObjectOutputALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the output object"}
}
}#FOREACH $Computer
Write-Verbose -Message "ADSI Search completed"
}#TRY
CATCH{
Write-Warning -Message "Something Wrong happened"
IF ($ErrProcessNewObjectSearcherALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the searcher object"}
IF ($ErrProcessNewObjectCredALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the alternate credential object"}
}#CATCH
}#ELSE
}#PROCESS
END{Write-Verbose -Message "Script Completed"}
}#function
#Get-Domaincomputer
#Get-Domaincomputer -ComputerName "LAB1*" -SizeLimit 5
#Get-Domaincomputer -Verbose -DomainDN 'DC=FX,DC=LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\Administrator")
#Get-Domaincomputer -Verbose -DomainDN 'FX.LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\FXtest")
#Get-Domaincomputer -DomainDN 'FX.LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\Administrator")
#Get-Domaincomputer -DomainDN 'FX.LAB' -ComputerName LAB1*
#Get-Domaincomputer -DomainDN 'LDAP://FX.LAB' -ComputerName LAB1*
#Get-Domaincomputer -DomainDN 'LDAP://DC=FX,DC=LAB' -ComputerName LAB1*