-
Notifications
You must be signed in to change notification settings - Fork 3
/
Set-ADUsersProxyAddresses.ps1
158 lines (123 loc) · 5.88 KB
/
Set-ADUsersProxyAddresses.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
function Set-ADUsersProxyAddresses {
<#
.SYNOPSIS
Set AD Proxy Addresses for Users
.DESCRIPTION
Set AD AD Proxy Addresses and primary mail address for AD users while clearing their existing or keeping the existing
.PARAMETER samaccountname
User's samaccountname
.PARAMETER PrimarySMTPAddress
User's primary send and receive SMTP address
.PARAMETER SIPAddress
User's SIP address, usually the same as their PrimarySMTPAddress
.PARAMETER ProxyAddresses
User's proxyaddresses
.PARAMETER X500Address
User's legacy X500 addresse
.PARAMETER KeepExistingProxyAddress
Switch to keep the users existing proxyaddresses
.EXAMPLE
Set-ADUsersProxyAddresses -samaccountname wkirkland-test -PrimarySMTPAddress [email protected] -SIPAddress [email protected] -ProxyAddresses '[email protected]','[email protected]'` -X500Address 'x500:/o=MEX08/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=6233b4d97389461d9791d7c8b555dfda-wesley.kirkland'
.NOTES
This is a easy way to one off update users proxy addresses
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,Position=0)]
[string]$samaccountname,
[Parameter(Mandatory=$false,Position=1)]
[string]$PrimarySMTPAddress = $null,
[Parameter(Mandatory=$false)]
[string]$SIPAddress = $null,
[Parameter(Mandatory=$false)]
[string[]]$ProxyAddresses = $null,
[Parameter(Mandatory=$false)]
[string]$X500Address = $null,
[Parameter(Mandatory=$false)]
[switch]$KeepExistingProxyAddress
)
Begin {
Write-Verbose 'Finding a DC to commit changes to'
$DC = Get-ADDomainController -Discover | Select-Object -ExpandProperty HostName
if (!(Test-Connection -Count 1 -ComputerName $DC)) {
Write-Error -Message "Unable to communicate to $DC, exiting now"
exit
}
}
Process {
if (!($KeepExistingProxyAddress)) {
Write-Verbose "We will clear all the proxy addresses for $samaccountname"
Set-ADUser -Identity $samaccountname -Clear proxyAddresses -Server $DC
}
#Make a blank object to store the new Addresses/Proxy Addresses to
$AddressesObject = New-Object -TypeName psobject
Write-Verbose "Were going to modify the Proxy Addresses for $samaccountname"
if ($PrimarySMTPAddress) {
Write-Verbose 'Found a Primary SMTP Address'
$AddressesObject | Add-Member -MemberType NoteProperty -Name 'mail' -Value "SMTP:$($PrimarySMTPAddress)"
}
if ($SIPAddress) {
Write-Verbose 'Found a SIP Address'
$AddressesObject | Add-Member -MemberType NoteProperty -Name 'SIP' -Value "SIP:$($SIPAddress)"
}
if ($ProxyAddresses) {
Write-Verbose 'Found Proxy Addresses'
[System.Collections.ArrayList]$ProxyAddressesTemp = @()
foreach ($Proxy in $ProxyAddresses) {
Try {
$ProxyAddressesTemp.Add(("smtp:$($Proxy)")) | Out-Null
}
Catch {
Write-Error 'Failed to add the proxy address to the ArrayList'
}
}
#Add the proxyaddresses to the array
$AddressesObject | Add-Member -MemberType NoteProperty -Name 'ProxyAddresses' -Value $ProxyAddressesTemp
}
if ($X500Address) {
Write-Verbose 'Found a X500 Address'
if (($X500Address.Split(':')[0]) -ceq 'x500') {
$AddressesObject | Add-Member -MemberType NoteProperty -Name 'X500' -Value $X500Address
} else {
$AddressesObject | Add-Member -MemberType NoteProperty -Name 'X500' -Value "x500:$($X500Address)"
}
}
if (($AddressesObject | Get-Member -MemberType NoteProperty).Count -gt 0) {
#Find proxy addresses to populate
$AttributesForProxyAddresses = $AddressesObject |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name
#Build a arraylist for the proxy addresses
[System.Collections.ArrayList]$ProxyAddressesArray = @()
if ($KeepExistingProxyAddress) {
$ExistingProxyAddresses = Get-ADUser -Identity $samaccountname -Properties ProxyAddresses -Server $DC | Select-Object -ExpandProperty ProxyAddresses
foreach ($Proxy in $ExistingProxyAddresses) {
Try {
$ProxyAddressesArray.Add($Proxy) | Out-Null
} Catch {
Write-Error "Failed to add existing $Proxy to ProxyAddressesArray"
}
}
}
foreach ($Attribute in $AttributesForProxyAddresses) {
$AttributeSplit = $AddressesObject.$Attribute.Split(',')
foreach ($Split in $AttributeSplit) {
Try {
$ProxyAddressesArray.Add($Split) | Out-Null
} Catch {
Write-Error "Failed to add $Attribute to ProxyAddressesArray"
}
}
}
Write-Verbose 'Removing duplicates from the ProxyAddressesArray'
#https://community.spiceworks.com/topic/1212728-converting-powershell-object-to-an-array-maybe
$ProxyAddressesArray = [string[]]($ProxyAddressesArray | Select-Object -Unique)
Set-ADUser -Identity $samaccountname -Replace (@{ProxyAddresses = $ProxyAddressesArray.ToArray()}) -Server $DC
if ($AddressesObject.mail) {
Set-ADUser -Identity $samaccountname -EmailAddress $PrimarySMTPAddress -Server $DC
}
}
}
End {}
}