-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-ComputerLocation.ps1
266 lines (230 loc) · 9.27 KB
/
Find-ComputerLocation.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<#
.SYNOPSIS
Enhanced script to cross-reference IP addresses against AD subnets (AD_Subnets.csv)
and find the corresponding site, plus do optional AD lookups.
.DESCRIPTION
1. Imports AD_Subnets.csv, which has columns: Subnet, SiteName, ...
2. Converts each subnet (CIDR) to a numeric range for easy matching.
3. Imports Computers.csv (ComputerFQDN, LastLoggedInUser, IpAddress).
4. For each record, determines the AD site by comparing IP address to subnets.
5. Optionally queries AD to confirm computer object details and user details.
6. Outputs the consolidated result, which can be saved to CSV.
.NOTES
Adjust domain controllers, credential usage, or advanced error handling
as needed for your environment.
.PARAMETER ComputerCsvPath
Path to the Computers.csv file.
.PARAMETER SubnetsCsvPath
Path to the AD_Subnets.csv file.
.PARAMETER OutputCsvPath
Where to store the final results (optional).
.EXAMPLE
.\Find-ComputerLocation.ps1
#>
param(
[string]$ComputerCsvPath = "C:\temp\Computers.csv",
[string]$SubnetsCsvPath = "C:\temp\AD_Subnets.csv",
[string]$OutputCsvPath = "C:\temp\FinalResults.csv"
)
################################################################################
# 1) HELPER FUNCTIONS
################################################################################
function ConvertTo-UInt32Ip {
<#
.SYNOPSIS
Converts a dotted-decimal IPv4 address (e.g. "10.10.10.5") to a uint32 number.
#>
param(
[Parameter(Mandatory)]
[ValidatePattern('^(\d{1,3}\.){3}\d{1,3}$')]
[string]$IpAddress
)
$octets = $IpAddress.Split('.')
return [uint32](($octets[0] -shl 24) -bor
($octets[1] -shl 16) -bor
($octets[2] -shl 8) -bor
$octets[3])
}
function ConvertCidrToRange {
<#
.SYNOPSIS
Converts a CIDR notation (e.g. "10.10.10.0/24") into a hashtable containing
the Network (lowest IP) and Broadcast (highest IP) in uint32 form.
#>
param(
[Parameter(Mandatory)]
[ValidatePattern('^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$')]
[string]$Cidr
)
$parts = $Cidr.Split('/')
$netIp = $parts[0]
$prefix = [int]$parts[1]
$netNum = ConvertTo-UInt32Ip -IpAddress $netIp
$mask = [uint32](0xFFFFFFFF -shl (32 - $prefix))
# Network address (lowest IP in the subnet)
$network = $netNum -band $mask
# Broadcast address (highest IP in the subnet)
$broadcast = $network -bor ([uint32]~$mask)
return @{
Network = $network
Broadcast = $broadcast
Prefix = $prefix
}
}
function Test-IpInRange {
<#
.SYNOPSIS
Checks if a given IP numeric (uint32) is between the specified Network and Broadcast.
#>
param(
[Parameter(Mandatory)] [uint32]$IpNum,
[Parameter(Mandatory)] [uint32]$Network,
[Parameter(Mandatory)] [uint32]$Broadcast
)
return ($IpNum -ge $Network -and $IpNum -le $Broadcast)
}
################################################################################
# 2) IMPORT AD_SUBNETS AND TRANSFORM
################################################################################
Write-Host "`n[Info] Importing AD Subnets from $SubnetsCsvPath"
$adSubnets = Import-Csv -Path $SubnetsCsvPath
# Convert each Subnet entry to numeric ranges
foreach ($row in $adSubnets) {
# For example, $row.Subnet might be "10.10.10.0/24"
if ($row.Subnet -match '/') {
try {
$range = ConvertCidrToRange -Cidr $row.Subnet
$row | Add-Member -NotePropertyName 'NetworkNum' -NotePropertyValue $range.Network
$row | Add-Member -NotePropertyName 'BroadcastNum' -NotePropertyValue $range.Broadcast
}
catch {
Write-Warning "Failed to parse subnet $($row.Subnet): $($_.Exception.Message)"
$row | Add-Member -NotePropertyName 'NetworkNum' -NotePropertyValue $null
$row | Add-Member -NotePropertyName 'BroadcastNum' -NotePropertyValue $null
}
}
else {
# If the format isn't CIDR, you can skip or handle differently
Write-Warning "Subnet $($row.Subnet) is not in CIDR format."
$row | Add-Member -NotePropertyName 'NetworkNum' -NotePropertyValue $null
$row | Add-Member -NotePropertyName 'BroadcastNum' -NotePropertyValue $null
}
}
################################################################################
# 3) IMPORT COMPUTERS.CSV AND PROCESS
################################################################################
Write-Host "`n[Info] Importing Computers from $ComputerCsvPath"
$computers = Import-Csv -Path $ComputerCsvPath
# Optional: Define domain controllers or domains to search
# You might have multiple domains. Adjust as needed.
$domainControllers = @("dc1.domainA.local","dc2.domainB.local")
# We'll build an array of final results
$results = @()
foreach ($entry in $computers) {
$computerFQDN = $entry.ComputerFQDN
$lastLoggedInUser = $entry.LastLoggedInUser
$ipAddress = $entry.IpAddress
Write-Host "`nProcessing: $computerFQDN (User: $lastLoggedInUser, IP: $ipAddress)"
# Default site name in case no matching subnet is found
$siteName = "Unknown"
# 3a) Find the AD site by IP (if valid IPv4)
if ($ipAddress -and $ipAddress -match '^(\d{1,3}\.){3}\d{1,3}$') {
$ipNum = ConvertTo-UInt32Ip -IpAddress $ipAddress
foreach ($subnet in $adSubnets) {
if ($subnet.NetworkNum -and $subnet.BroadcastNum) {
# Check if IP falls in this subnet
if (Test-IpInRange -IpNum $ipNum -Network $subnet.NetworkNum -Broadcast $subnet.BroadcastNum) {
$siteName = $subnet.SiteName
break
}
}
}
}
# 3b) (Optional) Query AD for the computer object
# We try each domain controller or domain until we find a match.
$adComputer = $null
if ($computerFQDN) {
$shortName = $computerFQDN.Split(".")[0]
foreach ($dc in $domainControllers) {
try {
# Attempt exact FQDN match
$adComputer = Get-ADComputer -Server $dc -Filter "DNSHostName -eq '$computerFQDN'" -Properties *
if (-not $adComputer) {
# Fallback to short name
$adComputer = Get-ADComputer -Server $dc -Filter "Name -eq '$shortName'" -Properties *
}
if ($adComputer) {
Write-Host " Found computer in domain: $($adComputer.DistinguishedName)"
break
}
} catch {
Write-Warning " Error querying DC $dc for $computerFQDN: $($_.Exception.Message)"
}
}
}
# Gather computer data if found
$computerOU = $null
$operatingSystem = $null
$lastLogonDate = $null
if ($adComputer) {
$computerOU = $adComputer.CanonicalName
$operatingSystem = $adComputer.OperatingSystem
$lastLogonDate = $adComputer.LastLogonDate
}
# 3c) (Optional) Query AD for the last logged in user
# This logic assumes either UPN or DOMAIN\Username
$adUser = $null
if ($lastLoggedInUser) {
# Parse if we have domain\username
if ($lastLoggedInUser -match '\\') {
$parts = $lastLoggedInUser.Split('\')
$userDomain = $parts[0]
$userAccount = $parts[1]
$candidate = "$userDomain\$userAccount"
}
elseif ($lastLoggedInUser -match '@') {
$candidate = $lastLoggedInUser
}
else {
# Just a username, no domain info
$candidate = $lastLoggedInUser
}
# Attempt to find the user in AD.
# You might also need to loop through domain controllers if truly multi-domain.
try {
$adUser = Get-ADUser -Identity $candidate -Properties DisplayName, Department, EmailAddress -ErrorAction SilentlyContinue
if ($adUser) {
Write-Host " Found user: $($adUser.SamAccountName)"
}
else {
Write-Host " No AD user found for: $($lastLoggedInUser)"
}
}
catch {
Write-Warning " Error searching for user $lastLoggedInUser: $($_.Exception.Message)"
}
}
# Build a result object
$obj = [PSCustomObject]@{
ComputerFQDN = $computerFQDN
IpAddress = $ipAddress
AD_Site = $siteName
ComputerOU = $computerOU
OperatingSystem = $operatingSystem
LastLogonDate = $lastLogonDate
LastLoggedInUser = $lastLoggedInUser
UserSamAccount = $adUser?.SamAccountName
UserDisplayName = $adUser?.DisplayName
UserDepartment = $adUser?.Department
}
$results += $obj
}
################################################################################
# 4) OUTPUT THE RESULTS
################################################################################
Write-Host "`n[Info] Processing complete. Summary of results:"
$results | Format-Table -AutoSize
Write-Host "`n[Info] Exporting final results to $OutputCsvPath"
$results | Export-Csv -Path $OutputCsvPath -NoTypeInformation
Write-Host "[Info] Done."