forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ProxyAddresses.ps1
75 lines (61 loc) · 1.99 KB
/
Get-ProxyAddresses.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
# to get primary or secondary
# $addresses = Get-ProxyAddresses | select *, @{n='IsSecondary';e={if ($_.addr.startswith('smtp')) {$true} else {$false}}}
# to get those that do not have a secondary
# $addresses | group name | ? count -eq 1 | % group
function Get-ProxyAddresses {
Connect-AzureAD | Out-Null
#$accts = $(Get-MsolUser -All; Get-MsolGroup -All)
$accts = $(Get-AzureADUser -All:$true; Get-AzureADGroup -All:$true)
$(foreach ($acct in $accts) {
if ($acct.lastdirsynctime) {
$sync = 'AD'
} else {
$sync = 'o365'
}
if ($acct.signinname) {
$type = 'User'
$signinname = [pscustomobject]@{
Name = $acct.DisplayName
Sync = $sync
Type = $type
Addr = $acct.signinname.trim()
}
} else {
$type = 'Group'
}
if ($acct.userprincipalname) {
$upn = [pscustomobject]@{
Name = $acct.DisplayName
Sync = $sync
Type = $type
Addr = $acct.userprincipalname.trim()
}
}
$proxy = @(foreach ($addr in $acct.proxyaddresses) {
[pscustomobject]@{
Name = $acct.displayname
Sync = $sync
Type = $type
Addr = $addr.trim()
}
})
if (('smtp:' + $signinname.Addr) -notin $proxy.addr) {
$proxy += $signinname
}
if (('smtp:' + $upn.addr) -notin $proxy.addr) {
$proxy += $upn
}
foreach ($altEA in $acct.AlternateEmailAddresses) {
$alt = [pscustomobject]@{
Name = $acct.DisplayName
Sync = $sync
Type = $type
Addr = $altEA.trim()
}
if (('smtp:' + $alt.addr) -notin $proxy.addr) {
$proxy += $alt
}
}
$proxy
}) | select * -Unique
}