-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrate-Students.ps1
134 lines (113 loc) · 4.61 KB
/
Migrate-Students.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
function Start-StudentMigration
{
<#
.Synopsis
Creates students in Microsoft Teams.
.Description
Reads ASV Data from Get-DataFromAsvXml and creates student user accounts based on ASV Data.
.Parameter data
Object returned from Get-DataFromAsvXml
.Parameter AADUserOutput
Path where output file of created users sould be stored.
.Parameter Format
UPN Format "{0}.{1}.{2}[email protected]"
{0} = Firstname
{1} = Lastname
{2} = Birthday
.Parameter PasswordListPath
Path to .csv file with exisiting passwords. Structure (vorname,nachname,pass)
.Parameter WhatIf
Does not create users in production system. Only prints users to console and creates output file.
.Example
# Creates students in asv.
Start-StudentMigration -data $data -AADUserOutput C:\users\docuemtns\created-students.csv -Format "{0}.{1}@myschool.tld"
#>
param
(
[parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
$data,
[Parameter(
Mandatory = $true
)]
$AADUserOutput,
[Parameter(
Mandatory = $true
)]
$Format,
$PassForAllUsers,
$PasswordListPath,
$WhatIf = $false
)
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.ForceChangePasswordNextLogin = $true
$LicensesToAssign = Get-LicensesToAssign -Plans @('M365EDU_A3_STUUSEBNFT')
if($null -ne $PasswordListPath)
{
$PasswordList = Load-PasswordList -passwordlistpath $PasswordListPath
}
$newAADUsers = @()
$aadusers = Get-AadUserHashTable
foreach ($k in $data.Klassen)
{
foreach ($kg in $k.KlassenGruppen)
{
foreach ($kl in $kg.Klassenliste)
{
$upn = Get-Upn -vorname ($kl.Vorname) -nachname ($kl.Familienname) -gebdat ($kl.GebDatum) -klasse ($k.Klassenname) -format $Format
if (!$aadusers.ContainsKey($upn))
{
$klasse = $k.Klassenname
$anrede = $kl.Anschriftstext
$anschrift = $kl.Strasse
$hsnr = $kl.HausNummer
$plz = $kl.PLZ
$ort = $kl.Ort
$oldflag = $false
$vorname = $kl.Vorname
$nachname = $kl.Familienname
$gebdat = $kl.GebDatum
if($PassForAllUsers -ne $null)
{$pass = $PassForAllUsers} else {$pass = (Get-RandomPassword(11).ToString()) + '!'}
$luser = New-Object psobject
# If password list is set and key with firstlastname exisists stored password
if($null -ne $PasswordListPath)
{
if($null -ne ($PasswordList.("$($vorname)$($nachname)") ) )
{
$pass = ($PasswordList.("$($vorname)$($nachname)")).ToString()
$oldflag = $true
}
}
$PasswordProfile.Password = $pass
if(!$WhatIf)
{
Write-Host "[CREATE] Create user $vorname $nachname"
$aad = New-AzureADUser -DisplayName ("$vorname $nachname") -GivenName $vorname -Surname $nachname -UserPrincipalName $upn -PasswordProfile $PasswordProfile -MailNickName $upn.Split('@')[0] -AccountEnabled $true -UsageLocation DE
Set-AzureADUserLicense -ObjectId $aad.ObjectId -AssignedLicenses $LicensesToAssign
$luser | Add-Member -MemberType NoteProperty -Name UPN -Value $aad.UserPrincipalName
} else {
Write-Host "[WHATIF] Create user $vorname $nachname"
$luser | Add-Member -MemberType NoteProperty -Name UPN -Value $upn
}
$luser | Add-Member -MemberType NoteProperty -Name Pass -Value $pass
$luser | Add-Member -MemberType NoteProperty -Name Nachname -Value $nachname
$luser | Add-Member -MemberType NoteProperty -Name Vorname -Value $vorname
$luser | Add-Member -MemberType NoteProperty -Name Geburtsdatum -Value $gebdat
$luser | Add-Member -MemberType NoteProperty -Name Klasse -Value $klasse
$luser | Add-Member -MemberType NoteProperty -Name Anrede -Value $anrede
$luser | Add-Member -MemberType NoteProperty -Name Anschrift -Value $anschrift
$luser | Add-Member -MemberType NoteProperty -Name Hausnummer -Value $hsnr
$luser | Add-Member -MemberType NoteProperty -Name PLZ -Value $plz
$luser | Add-Member -MemberType NoteProperty -Name Ort -Value $ort
$luser | Add-Member -MemberType NoteProperty -Name OldFlag -Value $oldflag
$newAADUsers += $luser
}
}
}
}
$newAADUsers | Export-Csv -Path $AADUserOutput -Encoding UTF8
return
}