Skip to content

Commit 30dd661

Browse files
committed
copy file from technet
1 parent ee4457f commit 30dd661

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
Function Get-DomainComputer {
2+
<#
3+
.SYNOPSIS
4+
The Get-DomainComputer function allows you to get information from an Active Directory Computer object using ADSI.
5+
6+
.DESCRIPTION
7+
The Get-DomainComputer function allows you to get information from an Active Directory Computer object using ADSI.
8+
You can specify: how many result you want to see, which credentials to use and/or which domain to query.
9+
10+
.PARAMETER ComputerName
11+
Specifies the name(s) of the Computer(s) to query
12+
13+
.PARAMETER SizeLimit
14+
Specifies the number of objects to output. Default is 100.
15+
16+
.PARAMETER DomainDN
17+
Specifies the path of the Domain to query.
18+
Examples: "FX.LAB"
19+
"DC=FX,DC=LAB"
20+
"Ldap://FX.LAB"
21+
"Ldap://DC=FX,DC=LAB"
22+
23+
.PARAMETER Credential
24+
Specifies the alternate credentials to use.
25+
26+
.EXAMPLE
27+
Get-DomainComputer
28+
29+
This will show all the computers in the current domain
30+
31+
.EXAMPLE
32+
Get-DomainComputer -ComputerName "Workstation001"
33+
34+
This will query information for the computer Workstation001.
35+
36+
.EXAMPLE
37+
Get-DomainComputer -ComputerName "Workstation001","Workstation002"
38+
39+
This will query information for the computers Workstation001 and Workstation002.
40+
41+
.EXAMPLE
42+
Get-Content -Path c:\WorkstationsList.txt | Get-DomainComputer
43+
44+
This will query information for all the workstations listed inside the WorkstationsList.txt file.
45+
46+
.EXAMPLE
47+
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -Verbose
48+
49+
This will query information for computers starting with 'Workstation0', but only show 10 results max.
50+
The Verbose parameter allow you to track the progression of the script.
51+
52+
.EXAMPLE
53+
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -Verbose -DomainDN "DC=FX,DC=LAB" -Credential (Get-Credential -Credential FX\Administrator)
54+
55+
This will query information for computers starting with 'Workstation0' from the domain FX.LAB with the account FX\Administrator.
56+
Only show 10 results max and the Verbose parameter allows you to track the progression of the script.
57+
58+
.NOTES
59+
NAME: FUNCT-AD-COMPUTER-Get-DomainComputer.ps1
60+
AUTHOR: Francois-Xavier CAT
61+
DATE: 2013/10/26
62+
WWW: www.lazywinadmin.com
63+
TWITTER: @lazywinadmin
64+
65+
VERSION HISTORY:
66+
1.0 2013.10.26
67+
Initial Version
68+
#>
69+
70+
[CmdletBinding()]
71+
PARAM(
72+
[Parameter(
73+
ValueFromPipelineByPropertyName=$true,
74+
ValueFromPipeline=$true)]
75+
[Alias("Computer")]
76+
[String[]]$ComputerName,
77+
78+
[Alias("ResultLimit","Limit")]
79+
[int]$SizeLimit='100',
80+
81+
[Parameter(ValueFromPipelineByPropertyName=$true)]
82+
[Alias("Domain")]
83+
[String]$DomainDN=$(([adsisearcher]"").Searchroot.path),
84+
85+
[Alias("RunAs")]
86+
[System.Management.Automation.Credential()]
87+
$Credential = [System.Management.Automation.PSCredential]::Empty
88+
89+
)#PARAM
90+
91+
PROCESS{
92+
IF ($ComputerName){
93+
Write-Verbose -Message "One or more ComputerName specified"
94+
FOREACH ($item in $ComputerName){
95+
TRY{
96+
# Building the basic search object with some parameters
97+
Write-Verbose -Message "COMPUTERNAME: $item"
98+
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectSearcher
99+
$Searcher.Filter = "(&(objectCategory=Computer)(name=$item))"
100+
$Searcher.SizeLimit = $SizeLimit
101+
$Searcher.SearchRoot = $DomainDN
102+
103+
# Specify a different domain to query
104+
IF ($PSBoundParameters['DomainDN']){
105+
IF ($DomainDN -notlike "LDAP://*") {$DomainDN = "LDAP://$DomainDN"}#IF
106+
Write-Verbose -Message "Different Domain specified: $DomainDN"
107+
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['DomainDN'])
108+
109+
# Alternate Credentials
110+
IF ($PSBoundParameters['Credential']) {
111+
Write-Verbose -Message "Different Credential specified: $($Credential.UserName)"
112+
$Domain = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password) -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectCred
113+
$Searcher.SearchRoot = $Domain}#IF ($PSBoundParameters['Credential'])
114+
115+
# Querying the Active Directory
116+
Write-Verbose -Message "Starting the ADSI Search..."
117+
FOREACH ($Computer in $($Searcher.FindAll())){
118+
Write-Verbose -Message "$($Computer.properties.name)"
119+
New-Object -TypeName PSObject -ErrorAction 'Continue' -ErrorVariable ErrProcessNewObjectOutput -Property @{
120+
"Name" = $($Computer.properties.name)
121+
"DNShostName" = $($Computer.properties.dnshostname)
122+
"Description" = $($Computer.properties.description)
123+
"OperatingSystem"=$($Computer.Properties.operatingsystem)
124+
"WhenCreated" = $($Computer.properties.whencreated)
125+
"DistinguishedName" = $($Computer.properties.distinguishedname)}#New-Object
126+
}#FOREACH $Computer
127+
128+
Write-Verbose -Message "ADSI Search completed"
129+
}#TRY
130+
CATCH{
131+
Write-Warning -Message ('{0}: {1}' -f $item, $_.Exception.Message)
132+
IF ($ErrProcessNewObjectSearcher){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the searcher object"}
133+
IF ($ErrProcessNewObjectCred){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the alternate credential object"}
134+
IF ($ErrProcessNewObjectOutput){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the output object"}
135+
}#CATCH
136+
}#FOREACH $item
137+
138+
139+
}#IF $ComputerName
140+
ELSE {
141+
Write-Verbose -Message "No ComputerName specified"
142+
TRY{
143+
# Building the basic search object with some parameters
144+
Write-Verbose -Message "List All object"
145+
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectSearcherALL
146+
$Searcher.Filter = "(objectCategory=Computer)"
147+
$Searcher.SizeLimit = $SizeLimit
148+
149+
# Specify a different domain to query
150+
IF ($PSBoundParameters['DomainDN']){
151+
$DomainDN = "LDAP://$DomainDN"
152+
Write-Verbose -Message "Different Domain specified: $DomainDN"
153+
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['DomainDN'])
154+
155+
# Alternate Credentials
156+
IF ($PSBoundParameters['Credential']) {
157+
Write-Verbose -Message "Different Credential specified: $($Credential.UserName)"
158+
$DomainDN = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN, $Credential.UserName,$Credential.GetNetworkCredential().password -ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectCredALL
159+
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['Credential'])
160+
161+
# Querying the Active Directory
162+
Write-Verbose -Message "Starting the ADSI Search..."
163+
FOREACH ($Computer in $($Searcher.FindAll())){
164+
TRY{
165+
Write-Verbose -Message "$($Computer.properties.name)"
166+
New-Object -TypeName PSObject -ErrorAction 'Continue' -ErrorVariable ErrProcessNewObjectOutputALL -Property @{
167+
"Name" = $($Computer.properties.name)
168+
"DNShostName" = $($Computer.properties.dnshostname)
169+
"Description" = $($Computer.properties.description)
170+
"OperatingSystem"=$($Computer.Properties.operatingsystem)
171+
"WhenCreated" = $($Computer.properties.whencreated)
172+
"DistinguishedName" = $($Computer.properties.distinguishedname)}#New-Object
173+
}#TRY
174+
CATCH{
175+
Write-Warning -Message ('{0}: {1}' -f $Computer, $_.Exception.Message)
176+
IF ($ErrProcessNewObjectOutputALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the output object"}
177+
}
178+
}#FOREACH $Computer
179+
180+
Write-Verbose -Message "ADSI Search completed"
181+
182+
}#TRY
183+
184+
CATCH{
185+
Write-Warning -Message "Something Wrong happened"
186+
IF ($ErrProcessNewObjectSearcherALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the searcher object"}
187+
IF ($ErrProcessNewObjectCredALL){Write-Warning -Message "PROCESS BLOCK - Error during the creation of the alternate credential object"}
188+
189+
}#CATCH
190+
}#ELSE
191+
}#PROCESS
192+
END{Write-Verbose -Message "Script Completed"}
193+
}#function
194+
195+
#Get-Domaincomputer
196+
#Get-Domaincomputer -ComputerName "LAB1*" -SizeLimit 5
197+
#Get-Domaincomputer -Verbose -DomainDN 'DC=FX,DC=LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\Administrator")
198+
#Get-Domaincomputer -Verbose -DomainDN 'FX.LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\FXtest")
199+
#Get-Domaincomputer -DomainDN 'FX.LAB' -ComputerName LAB1* -Credential (Get-Credential -Credential "FX.LAB\Administrator")
200+
#Get-Domaincomputer -DomainDN 'FX.LAB' -ComputerName LAB1*
201+
#Get-Domaincomputer -DomainDN 'LDAP://FX.LAB' -ComputerName LAB1*
202+
#Get-Domaincomputer -DomainDN 'LDAP://DC=FX,DC=LAB' -ComputerName LAB1*

0 commit comments

Comments
 (0)