-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResetUserData.ps1
292 lines (275 loc) · 12.2 KB
/
ResetUserData.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
## ------------------------------------------
##
##Script: ResetUserData Script
##Version: V4
##Author: Tiago Roxo
##
## ------------------------------------------
Clear-Host
Function SkypeResetUserData () {
#USERS MENU
do{
Clear-Host
Write-Host "Choose a valid option:" -ForegroundColor White -BackgroundColor DarkGreen
Write-Host "[1]Import users from file"
Write-Host "[2]Import users frim Get-CsUser"
$input = Read-Host 'Type...'
}while (($input -notmatch 1) -and ($input -notmatch 2))
#USERS FILTER
if($input -eq 1){
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath("Desktop")
}
$null = $FileBrowser.ShowDialog()
$allusers = Get-Content $FileBrowser.FileName.ToString()
}else{
$allusers = Get-CsUser
}
#WORK
Write-Host $allusers.count " Users found to be processed on this list" -ForegroundColor Red -BackgroundColor Yellow
[void](Read-Host 'Press Enter to Start… or CTRL+C to Cancel')
Foreach ($id in $allusers){
ResetUserData -identity $id
}
##--Opens the Folder where the backups were saved.
explorer.exe $($folder)
Write-Host $allusers.count " Users analysed and proccessed." -ForegroundColor White -BackgroundColor Green
}
##-- FUNCTION START
Function ResetUserData () {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$identity
)
##--VAR
$user = Get-CsUser -identity $identity
$sip=$user.SipAddress.Substring(4)
$hour = (get-date).hour
$minute = (get-date).minute
$second = (get-date).Second
##--Create folder
##https://www.sapien.com/blog/2018/03/22/storing-powershell-variables-in-external-files/
$folder = "C:\UserDataBackup\"
if( -Not (Test-Path -Path $folder ) )
{
New-Item -ItemType directory -Path $folder
Write-Host "Creating folder" $folder -ForegroundColor Yellow -BackgroundColor DarkGreen
}else{
}
$file = $folder+"ExportedUserData"+$user.SamAccountName.ToString() +$hour +$minute +$second +".zip"
##--START
##--Check if users it's Hosted On-Premises or Online
if ($user.Enabled.ToString() -eq "True"){
if ($user.HostingProvider.ToString() -ne "sipfed.online.lync.com")
{
Write-Host "Starting the procces for the account->" $user.SipAddress.ToString() -ForegroundColor White -BackgroundColor Green
Write-Host "Start Export-CsUserData for the user->" $user.SipAddress.ToString() "Hosted on Pool->" $user.RegistrarPool.ToString() -ForegroundColor Yellow -BackgroundColor DarkGreen
##--Uncomment the line below if you want to check user by user by pressing enter.
#[void](Read-Host 'Press Enter to continue…')
Export-CsUserData -PoolFqdn $user.RegistrarPool.ToString() -FileName $file -UserFilter $sip -Verbose
Write-Host "Finished Export-CsUserData"
Write-Host "loading..."
Start-Sleep -s 5
Write-Host "Start Disable-csuser for the user->" $user.SipAddress.ToString() -ForegroundColor Yellow -BackgroundColor DarkGreen
Disable-CsUser -Identity $sip -Verbose
invoke-csmanagementstorereplication
Write-Host "Finished Disable-CsUser"
Write-Host "loading..."
Start-Sleep -s 30
Write-Host "Start Enable-CsUser->" $user.SipAddress.ToString() -ForegroundColor Yellow -BackgroundColor DarkGreen
Enable-CsUser -Identity $sip -RegistrarPool $user.RegistrarPool.ToString() -SipAddress $user.SipAddress.ToString() -Verbose
Write-Host "Finished Enable-CsUser"
Write-Host "loading..."
Start-Sleep -s 5
Write-Host "Start Update-CsUserData->" $user.SipAddress.ToString() -ForegroundColor Yellow -BackgroundColor DarkGreen
Update-CsUserData -Filename $file -UserFilter $user -Verbose
Write-Host "Finished Update-CsUserData"
Write-Host "loading..."
Write-Host "-------------------------- USER DETAILS[BEFORE] --------------------------"
Get-csuser -Identity $identity
Write-Host "-------------------------- USER DETAILS[BEFORE] --------------------------"
Start-Sleep -s 5
Write-Host "Start Restoring Policies" -ForegroundColor Yellow -BackgroundColor DarkGreen
try
{
if ($user.Clientpolicy){
Grant-CsClientPolicy -Identity $sip -PolicyName $user.Clientpolicy.toString()
}
}
catch
{
Grant-CsClientPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.Clientpolicy.toString()
}
try
{
if ($user.VoicePolicy){
Grant-CsVoicePolicy -Identity $sip -PolicyName $user.VoicePolicy.toString()
}
}
catch
{
Grant-CsVoicePolicy -Identity $user.SipAddress.ToString() -PolicyName $user.VoicePolicy.toString()
}
try
{
if ($user.VoiceRoutingPolicy){
Grant-CsVoiceRoutingPolicy -Identity $sip -PolicyName $user.VoiceRoutingPolicy.toString()
}
}
catch
{
Grant-CsVoiceRoutingPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.VoiceRoutingPolicy.toString()
}
try
{
if ($user.ConferencingPolicy){
Grant-CsConferencingPolicy -Identity $sip -PolicyName $user.ConferencingPolicy.toString()
}
}
catch
{
Grant-CsConferencingPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.ConferencingPolicy.toString()
}
try
{
if ($user.PresencePolicy){
Grant-CsPresencePolicy -Identity $sip -PolicyName $user.PresencePolicy.toString()
}
}
catch
{
Grant-CsPresencePolicy -Identity $user.SipAddress.ToString() -PolicyName $user.PresencePolicy.toString()
}
try
{
if ($user.DialPlan){
Grant-CsDialPlan -Identity $sip -PolicyName $user.DialPlan.toString()
}
}
catch
{
Grant-CsDialPlan -Identity $user.SipAddress.ToString() -PolicyName $user.DialPlan.toString()
}
try
{
if ($user.LocationPolicy){
Grant-CsLocationPolicy -Identity $sip -PolicyName $user.LocationPolicy.toString()
}
}
catch
{
Grant-CsLocationPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.LocationPolicy.toString()
}
try
{
if ($user.ClientVersionPolicy){
Grant-CsClientVersionPolicy -Identity $sip -PolicyName $user.ClientVersionPolicy.toString()
}
}
catch
{
Grant-CsClientVersionPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.ClientVersionPolicy.toString()
}
try
{
if ($user.ArchivingPolicy){
Grant-CsArchivingPolicy -Identity $sip -PolicyName $user.ArchivingPolicy.toString()
}
}
catch
{
Grant-CsArchivingPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.ArchivingPolicy.toString()
}
try
{
if ($user.UserServicesPolicy){
Grant-CsUserServicesPolicy -Identity $sip -PolicyName $user.UserServicesPolicy.toString()
}
}
catch
{
Grant-CsUserServicesPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.UserServicesPolicy.toString()
}
try
{
if ($user.CallViaWorkPolicy){
Grant-CsCallViaWorkPolicy -Identity $sip -PolicyName $user.CallViaWorkPolicy.toString()
}
}
catch
{
Grant-CsCallViaWorkPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.CallViaWorkPolicy.toString()
}
try
{
if ($user.ThirdPartyVideoSystemPolicy){
Grant-CsThirdPartyVideoSystemPolicy -Identity $sip -PolicyName $user.ThirdPartyVideoSystemPolicy.toString()
}
}
catch
{
Grant-CsThirdPartyVideoSystemPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.ThirdPartyVideoSystemPolicy.toString()
}
try
{
if ($user.MobilityPolicy){
Grant-CsMobilityPolicy -Identity $sip -PolicyName $user.MobilityPolicy.toString()
}
}
catch
{
Grant-CsMobilityPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.MobilityPolicy.toString()
}
try
{
if ($user.HostedVoicemailPolicy){
Grant-CsHostedVoicemailPolicy -Identity $sip -PolicyName $user.HostedVoicemailPolicy.toString()
}
}
catch
{
Grant-CsHostedVoicemailPolicy -Identity $user.SipAddress.ToString() -PolicyName $user.HostedVoicemailPolicy.toString()
}
try
{
if ($user.TeamsUpgradePolicy){
Grant-CsTeamsUpgradePolicy -Identity $sip -PolicyName $user.TeamsUpgradePolicy.toString()
}
}
catch
{
Grant-CsTeamsUpgradePolicy -Identity $user.SipAddress.ToString() -PolicyName $user.TeamsUpgradePolicy.toString()
}
try
{
if ($user.IPPhonePolicy){
Grant-CsIPPhonePolicy -Identity $sip -PolicyName $user.IPPhonePolicy.toString()
}
}
catch
{
Grant-CsIPPhonePolicy -Identity $user.SipAddress.ToString() -PolicyName $user.IPPhonePolicy.toString()
}
try
{
Set-CsUser –Identity $sip –LineUri $user.LineUri.toString() -EnterpriseVoiceEnabled $user.EnterpriseVoiceEnabled
}
catch
{
Set-CsUser –Identity $user.SipAddress.ToString() –LineUri $user.LineUri.toString() -EnterpriseVoiceEnabled $user.EnterpriseVoiceEnabled
}
Write-Host "Finish Restoring Policies" -ForegroundColor White -BackgroundColor Green
Write-Host "-------------------------- USER DETAILS[AFTER] --------------------------"
Get-csuser -Identity $identity
Write-Host "-------------------------- USER DETAILS[AFTER] --------------------------"
Start-Sleep -s 5
}
else{
Write-Host "Cannot process user - > USER" $user.SipAddress.ToString() "it's hosted Online - >" $user.HostingProvider.ToString() -ForegroundColor White -BackgroundColor Red
}
}else{
Write-Host "Cannot process user - > USER" $user.SipAddress.ToString() "user is Disabled" -ForegroundColor White -BackgroundColor Red
}
}
SkypeResetUserData