-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathCreate-ContactsOnAD.ps1
113 lines (93 loc) · 4.16 KB
/
Create-ContactsOnAD.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
Add-Type -AssemblyName System.Windows.Forms
$PathDesktop = 'C:' + $env:HOMEPATH + '\Desktop'
$ContactOU = 'OU=Contacts,OU=Utilisateurs,DC=contoso,DC=local'
$DomainController = 'DomainController1'
function Select-FileDialog
{
[CmdletBinding()]
param ([string]$Title,
[string]$Filter = 'All files *.*|*.*')
Add-Type -AssemblyName System.Windows.Forms | Out-Null
$fileDialogBox = New-Object -TypeName Windows.Forms.OpenFileDialog
$fileDialogBox.ShowHelp = $false
$fileDialogBox.initialDirectory = $PathDesktop
$fileDialogBox.filter = $Filter
$fileDialogBox.Title = $Title
$Show = $fileDialogBox.ShowDialog()
if ($Show -eq 'OK')
{
Return $fileDialogBox.FileName
}
Else
{
Write-Error -Message 'Opération annulée'
[Windows.Forms.MessageBox]::Show("Le script n'est pas en mesure de continuer. Opération stoppée.", 'Opération stoppée', 0, [Windows.Forms.MessageBoxIcon]::Error)
Stop-TranscriptOnLog
Exit
}
}
# Disclaimer
$Disclaimer = [Windows.Forms.MessageBox]::Show(
"
Ce script a pour but de rajouter des contacts dans l'Active Directory.
Pour cela, il injecte des données venant d'un fichier .csv.
/!\ Attention /!\
Si vous n'êtes pas sûr des actions à mener ou de l'impact, quitter ce script dès à présent.
Souhaitez-vous continuer ?
", "Active Directory - Ajout de contact", 1, [Windows.Forms.MessageBoxIcon]::Question)
If ($Disclaimer -eq "OK")
{
Write-Host 'Patientez, traitement en cours ...'
}
Else
{
Write-Error -Message 'Opération annulée'
[Windows.Forms.MessageBox]::Show("Le script n'est pas en mesure de continuer. Opération stoppée.", 'Opération stoppée', 0, [Windows.Forms.MessageBoxIcon]::Error)
Stop-TranscriptOnLog
Exit
}
Import-Module ActiveDirectory
##region Import information coming from input .CSV file
# Import CSV file
[Windows.Forms.MessageBox]::Show(
"
Sélectionner dans cette fenêtre le fichier contenant :
- Le 'Prénom' du contact à créer
- Le 'Nom' du contact à créer ()
- L'adresse e-mail du contact à créer
Le fichier doit être de la forme suivante :
Prénom NOM Mail
John DOE [email protected]
Jane ROE [email protected]
", "Office 365 - Ajout de contact", 0, [Windows.Forms.MessageBoxIcon]::Question)
# Import list of users and related sharedmailbox and rights
$CSVInputFile = Select-FileDialog -Title 'Select CSV file' -Filter 'Fichier CSV (*.csv) |*.csv'
$csvValues = Import-Csv $CSVInputFile -Delimiter ';'
#endregion Import information coming from input .CSV file
#region Apply modification
foreach ($line in $csvValues)
{
$UserFirstName = $line.'Prenom'
$UserLASTNAME = $line.'Nom'.ToUpper()
$UserName = $UserFirstName + ' ' + $UserLASTNAME
$UserDescription = $line.'Description'
$UserMailAddress = $line.'Mail'
$UserMobile = $line.'Mobile'
$UserTitle = $line.'Titre'
$UserDepartment = $line.'Service'
$UserCompanyOffice = $line.'Bureau'
$UserCompanyName = $line.'Societe'
$UserCompanyAddress = $line.'Adresse'
$UserCompanyCity = $line.'Ville'
$UserCompanyPostalCode = $line.'CP'
$UserCompanyPhoneLine = $line.'TelSociete'
$UserCompanyFax = $line.'FaxSociete'
Write-Host 'Création du contact' $UserName $UserMailAddress $UserMobile $UserTitle $UserDepartment $UserCompanyOffice $UserCompanyName $UserCompanyAddress $UserCompanyCity $UserCompanyPostalCode $UserCompanyPhoneLine $UserCompanyFax
New-ADObject -Type Contact -Server $DomainController -Name $UserName -Path $ContactOU -OtherAttributes @{ 'displayName' = $UserName; 'description' = $UserDescription; 'mail' = $UserMailAddress; 'proxyAddresses' = $UserMailAddress; 'givenName' = $UserFirstName; 'sn' = $UserLASTNAME; 'physicalDeliveryOfficeName' = $UserCompanyOffice; 'telephoneNumber' = $UserCompanyPhoneLine; 'mobile' = $UserMobile; 'facsimileTelephoneNumber' = $UserCompanyFax; 'streetAddress' = $UserCompanyAddress; 'l' = $UserCompanyCity; 'postalCode' = $UserCompanyPostalCode; 'c' = 'FR'; 'Title' = $UserTitle; 'department' = $UserDepartment; 'company' = $UserCompanyName }
}
#endregion Apply modification
#region Show MessageBox to confirm action
[Windows.Forms.MessageBox]::Show(
"Action menée avec succès.
", "ActiveDirectory - Ajout de contact", 0, [Windows.Forms.MessageBoxIcon]::Information)
#endregion Show MessageBox to confirm action