-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle_blokowanie_kont.ps1
91 lines (62 loc) · 2.46 KB
/
moodle_blokowanie_kont.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
<#
.SYNOPSIS
Blokowanie kont z moodla na podstawie danych z db
.DESCRIPTION
Niezbede do doinstalowania:
Install-Module -Name SqlServer
.NOTES
autor: hajduk 2019
#>
$sql = "SELECT [IDR]
,[Nazwisko]
,[Imie]
,[Status]
,[Wirtualny]
,[login]
,[Pracuje]
,[DataRozpoczeciaWspolpracy]
,[DataZakonczeniaWspolpracy]
,[EmailSluzbowy]
,[Telefon]
FROM [db].[dbo].[Pracownik]
WHERE AktualizujeIDR is NULL and
DataZakonczeniaWspolpracy >= DATEADD(day, -360, GETDATE())
AND DataZakonczeniaWspolpracy <= GETDATE()
Order by DataZakonczeniaWspolpracy DESC"
$server = "server"
#$database = ""
$username = "user"
$password = "password"
try {
$AccountToBlock = Invoke-Sqlcmd -Query $sql -ServerInstance $server -QueryTimeout 30 -ErrorAction 'Stop' -username $username -password $password
} catch {
$From = "[email protected]"
$To = "[email protected]"
$Subject = "Blad blokowania kont moodle"
$Body = $error
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$User = "[email protected]"
$password_mail = "Password" | ConvertTo-SecureString -asPlainText -Force
$cred=New-Object -TypeName System.Management.Automation.PSCredential($User,$password_mail)
$SMTPMessage = New-Object System.Net.Mail.MailMessage($From,$To,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
Write-Error "Job Failure" -EA Stop
}
$token = "token"
$auth = "manual"
$functionname_get = "core_user_get_users"
$functionname_block = "core_user_update_users"
$suspended = 1 # BLOKOWANIE USEROW
$AccountToBlock | % {
$login = $_.login
$url = "https://moodleaddress.pl/webservice/rest/server.php?wsfunction=$functionname_get&wstoken=$token&criteria[0][key]=username&criteria[0][value]=$login"
$response = Invoke-RestMethod -Method 'Post' -Uri $url | select OuterXml
[xml]$xml = $response.OuterXml
$id = $xml.RESPONSE.SINGLE.KEY[0].MULTIPLE.SINGLE.KEY[0].VALUE
$url = "https://moodleaddress.pl/webservice/rest/server.php?wsfunction=$functionname_block&wstoken=$token&moodlewsrestformat=json&users[0][id]=$id&users[0][suspended]=$suspended"
Invoke-RestMethod -Method 'Post' -Uri $url
}