-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-ADObjectLocation.psm1
66 lines (46 loc) · 1.69 KB
/
Get-ADObjectLocation.psm1
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
Function Get-ADObjectLocation {
Param (
[String]$ComputerName,
[String]$UserName
)
$Output = "`n`n"
if ($ComputerName) {
Try {
$PCInfo = Get-ADComputer $ComputerName -Properties CanonicalName
}
Catch {
write-host "`nError: Unable to query AD object $ComputerName." -ForegroundColor Red
Write-Host ("Below error was returend by the command`n" + $_.Exception.Message + "`n" + $_.Exception.ItemName) -ForegroundColor Red
break
}
$PCInfo.CanonicalName | foreach {
$item = $_.split('/')
for ($i = 0;$i -lt $item.count;$i++){
Write-host ($output + $item[$i])
$output = "|___ ".PadLeft($i+$i+5)
$OU = $item[$item.count - 2]
}
}
}
elseif ($UserName) {
Try {
$UserInfo = (Get-ADUser $UserName -Properties CanonicalName)
}
Catch {
write-host "`nError: Unable to query AD object `"$UserName`"." -ForegroundColor Red
Write-Host ("Below error was returend by the command`n" + $_.Exception.Message + "`n" + $_.Exception.ItemName) -ForegroundColor Red
break
}
$UserInfo.CanonicalName | foreach {
$item = $_.split('/')
for ($i = 0;$i -lt $item.count;$i++){
Write-host ($output + $item[$i])
$output = "|___ ".PadLeft($i+$i+5)
$OU = $item[$item.count - 2]
}
}
}
else {
Write-Host "`nError: Either ComputerName or UserName is expected, both cannot be left empty." -ForegroundColor Red
}
}