This repository has been archived by the owner on Mar 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusrs_activate.ps1
329 lines (280 loc) · 8.65 KB
/
usrs_activate.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Log function
# in file ".\date time.log"
# parameter - String e.g. "somestring".
# NB! string concatenation expression like this "black" + "cat"
# not applicable as parameter
function Logging([string]$log_str)
{
$today = Get-Date -Format "dd.MM.yyyy"
$dt = Get-Date -Format "dd.MM.yyyy HH:mm:ss"
$dt + ' - ' + $log_str | Out-File -FilePath ".\log\report-$today.log" -Append -Encoding unicode
}
# Transliteration function.
# string username "Surname NP"
# turn to email login
function Translit([string]$fio)
{
$alfavit = @{" "=""; "à"="a"; "á"="b"; "â"="v"; "ã"="g"; "ä"="d";
"å"="e"; "¸"="yo"; "æ"="zh"; "ç"="z";
"è"="i"; "é"="j"; "ê"="k"; "ë"="l";
"ì"="m"; "í"="n"; "î"="o"; "ï"="p";
"ð"="r"; "ñ"="s"; "ò"="t"; "ó"="u";
"ô"="f"; "õ"="kh"; "ö"="c"; "÷"="ch";
"ø"="sh"; "ù"="sch"; "ú"=""; "ü"="";
"û"="y"; "ý"="je"; "þ"="yu"; "ÿ"="ya"}
$alias = ""
foreach($c in $fio.ToCharArray())
{
if ($alfavit.ContainsKey($c.ToString())){
$alias = $alias + $alfavit[$c.ToString()]
}
else{
$alias = $alias + $c
}
}
return $alias
}
# Ôóíêöèÿ àêòèâàöèè ó÷åòíîé çàïèñè ïîëüçîâàòåëÿ.
# Ó÷åòíàÿ çàïèñü ÄÎËÆÍÀ ÑÓÙÅÑÒÂÎÂÀÒÜ.
# Àðãóìåíò '$san' - sAMAccountName èç ActiveDirectory
function Usr_activate($dist_name)
{
$u_object = Get-ADUser -Identity "$dist_name"
$log_str = "Âûçîâ ôóíêöèè îáðàáîòêè ïîëüçîâàòåëÿ " + $u_object.Name
Logging $log_str
if ($u_object.enabled -ne $true)
{
$chars = "abcdefghigkABCDEFGHIJK23456789!@$%*"
$chars = $chars.ToCharArray()
$pwd_string = ($chars | Get-Random -Count 7) -join ""
Logging " Ïàðîëü äëÿ ïîëüçîâàòåëÿ $u_object - $pwd_string"
try
{
$uObj = Get-ADUser -Identity "$dist_name"
Set-ADAccountPassword $uObj.distinguishedName -Reset -NewPassword (ConvertTo-SecureString `
-AsPlainText $pwd_string -Force)
$uObj = Get-ADUser -Identity "$dist_name"
Enable-ADAccount -Identity $uObj.distinguishedName
}
catch
{
$activate_err = " ERR: îøèáêà àêòèâàöèè
èëè íàçíà÷åíèÿ ïàðîëÿ - " + $Error[0].Exception.Message
Logging $activate_err
}
}
else
{
$msg = " Àêêàóíò " + $u_object.name + " àêòèâåí. Îáðàáîòêà ñëåäóþùåãî."
Logging $msg
}
}
# Ôóíêöèÿ îòêðûòèÿ ñåññèè Powershell
# íà ïî÷òîâîì ñåðâåðå (äëÿ èñïîëüçîâàíèÿ
# êîìàíäëåòîâ Exchange Management console)
function OpenSession
{
Logging "Âûçîâ ôóíêöèè îòêðûòèÿ ñåññèè"
$service = "[Exchange server account]"
try
{
$pwd = ConvertTo-SecureString -String (Get-Content ".\post")
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $service, $pwd
Logging " Ðàçðåøåíèå íà îòêðûòèå ñåññèè ñîçäàíî óñïåøíî."
}
catch
{
$cred_err = " ERR: îøèáêà ñîçäàíèÿ ðàçðåøåíèÿ,
ëèáî îòñóòñòâóåò ôàéë êëþ÷à - " + $Error[0].Exception.Message
Logging $cred_err
exit
}
try
{
Get-Credential -Credential $cred
$session = New-PSSession –ConfigurationName Microsoft.Exchange `
–ConnectionUri http://exchangehostname.domain.net/PowerShell/ `
–Credential $cred
Import-PSSession -Session $session `
-DisableNameChecking `
-CommandName Enable-Mailbox, Get-Mailbox
Logging " Ñåññèÿ îòêðûòà."
}
catch
{
$session_err = " ERR: îøèáêà îòêðûòèÿ ñåññèè - " + $Error[0].Exception.Message
Logging $session_err
exit
}
}
# Ôóíêöèÿ çàêðûòèÿ àêòèâíîé ñåññèè
# íà ñåðâåðå Exchange
function CloseSession
{
$active_sessions = Get-PSSession
if ($active_sessions)
{
try
{
Remove-PSSession
Logging "Ñåññèÿ íà ñåðâåðå Exchange çàêðûòà."
}
catch
{
$close_session_err = " ERR: îøèáêà ïðè ïîïûòêå çàêðûòü ñåññèþ - " +
$Error[0].Exception.Message
Logging $close_session_err
}
}
}
# Ôóíêöèÿ àêòèâàöèè (ñîçäàíèÿ) ïî÷òîâîãî ÿùèêà
# ñóùåñòâóþùåãî ïîëüçîâàòåëÿ.
# Àðãóìåíò '$san' - sAMAccountName èç ActiveDirectory
# Àðãóìåíò '$alias' - ðåçóëüòàò, âîçâðàùàåìûé
# ôóíêöèåé 'Translit'
function Mailbox_activate($dist_name, $alias)
{
$u = Get-ADUser -Identity "$dist_name"
$log_str = "Âûçîâ ôóíêöèè àêòèâàöèè ïî÷òîâîãî ÿùèêà ïîëüçîâàòåëÿ " + $u.Name
Logging $log_str
$mb = Get-Mailbox "$dist_name"
if ($mb.name -ne $null)
{
$msg = " Ïî÷òîâûé ÿùèê " + $mb.name + " óæå àêòèâèðîâàí.
Ïåðåõîä ê ñëåäóþùåìó."
Logging $msg
}
else
{
try
{
Enable-Mailbox -Identity "$dist_name" -Confirm:$false -Alias $alias
}
catch
{
$mb_activate_err = " Ïðè àêòèâàöèè ïî÷òîâîãî ÿùèêà " `
+ $mb.name + " ïðîèçîøëà îøèáêà - " `
+ $Error[0].Exception.Message
Logging $mb_activate_err
}
}
}
# Ôóíêöèÿ äëÿ çàïðîñîâ íà âûáîðêó äàííûõ PG
function Get-ODBC-Data{
param([string]$query)
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString = 'Driver={PostgreSQL 64-Bit ODBC Drivers};
Server=10.0.0.157;
Port=5432;
Database=postgres;
Uid=dev;
Pwd=1q2w3e$RT;'
$conn.open()
$cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
$ds = New-Object system.Data.DataSet
(New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
$conn.close()
$ds.Tables[0]
}
######################-Begin-######################
New-Item -ItemType directory log -Force | out-null
Logging "---Íà÷àëî âûïîëíåíèÿ çàäàíèÿ---"
Logging "=============================="
try
{
[diagnostics.process]::start(".\start.bat").WaitForExit()
}
catch
{
$start_putty_err = "ERR: Îøèáêà ïðè çàïóñêå Putty: " + $Error[0].Exception.Message
Logging "$start_putty_err"
exit
}
try
{
Import-Module ActiveDirectory
}
catch
{
$ad_module_err = "ERR: Îøèáêà ïðè èìïîðòå ìîäóëÿ AD: " + $Error[0].Exception.Message
Logging "$ad_module_err"
exit
}
OpenSession
$query = "SELECT au.distinguished_name
FROM personal.person_san ps
, personal.ad_users au
WHERE ps.person_id IN (
SELECT em.person_id FROM personal.employees em
WHERE (em.leave_date IS NULL OR current_date BETWEEN em.recept_date AND em.leave_date)
GROUP BY em.person_id )
AND ps.san = au.san"
try
{
Logging "Çàïðîñ íà âûáîðêó ïîëüçîâàòåëåé èç ÁÄ"
$result = Get-ODBC-Data -query $query
Logging "Çàïðîñ óñïåøíî çàâåðøåí"
}
catch
{
$db_err = "ERR: Îøèáêà ïðè âûïîëíåíèè çàïðîñà: " + $Error[0].Exception.Message
Logging "$db_err"
exit
}
if ($result.count -ne 0)
{
foreach ($dn in $result)
{
$curr_dn = $dn.distinguished_name
$current_user = Get-ADUser -Identity "$curr_dn"
$curr_san = $current_user.SamAccountName
$mail_alias = Translit $curr_san
Usr_activate $curr_dn
Mailbox_activate $curr_dn $mail_alias
}
}
else
{
Logging "Çàïðîñ ê ÁÄ âåðíóë ïóñòîé íàáîð!"
exit
}
CloseSession
Logging "=============================="
Logging "---The END---"
######################-Examples-######################
# Ôóíêöèÿ DML-çàïðîñîâ PG
<#function Set-ODBC-Data{
param([string]$query=$(throw 'query is required.'))
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString= 'Driver={PostgreSQL 64-Bit ODBC Drivers};
Server=10.0.0.157;
Port=5432;
Database=postgres;
Uid=dev;
Pwd=1q2w3e$RT;'
$conn.open()
$cmd = New-Object System.Data.Odbc.OdbcCommand($query,$conn)
$cmd.ExecuteNonQuery()
$conn.close()
}#>
#DML query example
<#$dml_query = "INSERT INTO personal.ad_groups
(gname
,san
,dn
cn)
VALUES
('Ïðîâåðî÷íàÿ ãðóïïà'
,'1'
,'2'
,'3')"
set-odbc-data -query $query#>
<#$u_object = Get-ADUser -Identity "CN=Usert1,OU=Test Users,DC=kursksmu,DC=net"
if ($u_object.enabled -ne $true)
{
Write-Host "disabled"
}
else
{
Write-Host "enabled!"
}#>