-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-DomainComputer.ps1
More file actions
121 lines (103 loc) · 4.77 KB
/
Get-DomainComputer.ps1
File metadata and controls
121 lines (103 loc) · 4.77 KB
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
function Get-DomainComputer {
<#
.EXAMPLE
Get-DomainComputer
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation001"
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation001", "Workstation002"
.EXAMPLE
Get-Content -Path "c:\WorkstationsList.txt" | Get-DomainComputer
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -Verbose
.EXAMPLE
Get-DomainComputer -ComputerName "Workstation0*" -SizeLimit 10 -DomainDN "DC=FX,DC=LAB" -Credential (Get-Credential)
.NOTES
Author: Ethan Blair
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[Alias("Computer")]
[string[]]$ComputerName,
[Alias("ResultLimit", "Limit")]
[int]$SizeLimit = 100,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("Domain")]
[string]$DomainDN = ([adsisearcher]"").SearchRoot.Path,
[Alias("RunAs")]
[System.Management.Automation.PSCredential]$Credential = [System.Management.Automation.PSCredential]::Empty
)
process {
if ($ComputerName) {
foreach ($computer in $ComputerName) {
try {
$searcher = New-Object System.DirectoryServices.DirectorySearcher -ErrorAction Stop
$searcher.Filter = "(&(objectCategory=Computer)(name=$computer))"
$searcher.SizeLimit = $SizeLimit
$searcher.SearchRoot = $DomainDN
if ($PSBoundParameters.ContainsKey('DomainDN')) {
if ($DomainDN -notlike "LDAP://*") {
$DomainDN = "LDAP://$DomainDN"
}
$searcher.SearchRoot = $DomainDN
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$domain = New-Object System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN, $Credential.UserName, $Credential.GetNetworkCredential().Password -ErrorAction Stop
$searcher.SearchRoot = $domain
}
foreach ($result in $searcher.FindAll()) {
[PSCustomObject]@{
Name = $result.Properties.name[0]
DNSHostName = $result.Properties.dnshostname[0]
Description = $result.Properties.description[0]
OperatingSystem = $result.Properties.operatingsystem[0]
WhenCreated = $result.Properties.whencreated[0]
DistinguishedName = $result.Properties.distinguishedname[0]
}
}
}
catch {
Write-Warning "$computer`: $($_.Exception.Message)"
}
}
}
else {
try {
$searcher = New-Object System.DirectoryServices.DirectorySearcher -ErrorAction Stop
$searcher.Filter = "(objectCategory=Computer)"
$searcher.SizeLimit = $SizeLimit
if ($PSBoundParameters.ContainsKey('DomainDN')) {
if ($DomainDN -notlike "LDAP://*") {
$DomainDN = "LDAP://$DomainDN"
}
$searcher.SearchRoot = $DomainDN
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$domain = New-Object System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN, $Credential.UserName, $Credential.GetNetworkCredential().Password -ErrorAction Stop
$searcher.SearchRoot = $domain
}
foreach ($result in $searcher.FindAll()) {
try {
[PSCustomObject]@{
Name = $result.Properties.name[0]
DNSHostName = $result.Properties.dnshostname[0]
Description = $result.Properties.description[0]
OperatingSystem = $result.Properties.operatingsystem[0]
WhenCreated = $result.Properties.whencreated[0]
DistinguishedName = $result.Properties.distinguishedname[0]
}
}
catch {
Write-Warning "Error processing computer $($result.Properties.name): $($_.Exception.Message)"
}
}
}
catch {
Write-Warning "Error during search operation: $($_.Exception.Message)"
}
}
}
end {
}
}