-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_config.ps1
172 lines (157 loc) · 5.08 KB
/
template_config.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
$interfaceName = "Wi-Fi"
$wifiName = "NOM_DU_RESEAU_WIFI_DU_FIELD" # Réseau permettant au robot de se connecter à la BaseStation
# Si le réseau wifi nécessite un mot de passe, reprendre la logique du script ./Univ/config_A_a_univ.ps1
# Demander à l'utilisateur de choisir un robot
do {
Write-Host ""
$robotChoice = Read-Host "Entrez numero robot (1-2-3-4-5)"
if ($robotChoice -notin '1','2','3','4','5') {
Write-Host ""
Write-Host "Choix invalide. Veuillez recommencer."
Write-Host ""
}
} while ($robotChoice -notin '1','2','3','4','5')
# Déterminer l'adresse IP en fonction du choix de l'utilisateur
switch ($robotChoice) {
1 { $ipAddressSuffix = 100 }
2 { $ipAddressSuffix = 101 }
3 { $ipAddressSuffix = 102 }
4 { $ipAddressSuffix = 103 }
5 { $ipAddressSuffix = 104 }
}
$ipAddress = "172.16.79.$ipAddressSuffix"
$subnetMask = 16 # Correspond à 255.255.0.0
$gateway = "172.16.1.1"
$dnsPrimary = "172.16.1.1"
$dnsSecondary = ""
Write-Host ""
# Désactiver IPv6
try {
Set-NetAdapterBinding -Name $interfaceName -ComponentID ms_tcpip6 -Enabled $False
Write-Host ""
Write-Host "IPv6 OFF"
Write-Host ""
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de desactiver IPv6. $_"
Write-Host ""
exit
}
# Supprimer toutes les configurations IP et passerelles existantes pour éviter les conflits
try {
Get-NetIPAddress -InterfaceAlias $interfaceName | ForEach-Object { Remove-NetIPAddress -InterfaceAlias $interfaceName -IPAddress $_.IPAddress -Confirm:$false }
Get-NetRoute -InterfaceAlias $interfaceName | Remove-NetRoute -Confirm:$false
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de supprimer les configurations IP ou les routes existantes. $_"
Write-Host ""
exit
}
# Configurer IPv4 avec une adresse IP statique et des paramètres de serveur DNS
try {
New-NetIPAddress -InterfaceAlias $interfaceName -IPAddress $ipAddress -PrefixLength $subnetMask -DefaultGateway $gateway
Set-DnsClientServerAddress -InterfaceAlias $interfaceName -ServerAddresses ($dnsPrimary, $dnsSecondary).Where({$_})
Write-Host ""
Write-Host "Configuration IP et DNS OK. Adresse IP: $ipAddress"
Write-Host ""
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de configurer l'adresse IP ou les serveurs DNS. $_"
Write-Host ""
exit
}
# Redémarrer l'adaptateur pour appliquer les changements
try {
Restart-NetAdapter -Name $interfaceName
Start-Sleep -Seconds 5
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de redemarrer l'adaptateur reseau. $_"
Write-Host ""
exit
}
# Ajouter un profil WiFi sans mot de passe
try {
$profileXml = @"
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>$wifiName</name>
<SSIDConfig>
<SSID>
<name>$wifiName</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>open</authentication>
<encryption>none</encryption>
<useOneX>false</useOneX>
</authEncryption>
</security>
</MSM>
</WLANProfile>
"@
$profilePath = [System.IO.Path]::Combine($env:TEMP, "$wifiName.xml")
[System.IO.File]::WriteAllText($profilePath, $profileXml)
netsh wlan add profile filename="$profilePath"
} catch {
Write-Host ""
Write-Host "Erreur : Impossible d'ajouter le profil WiFi. $_"
Write-Host ""
exit
}
# Vérifier si le réseau WiFi est disponible
try {
$availableNetworks = netsh wlan show networks interface="$interfaceName" | Select-String -Pattern $wifiName
if (-not $availableNetworks) {
Write-Host ""
Write-Host "Erreur : Wi-Fi $wifiName n'existe pas ou n'est pas disponible."
Write-Host ""
exit
}
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de tester la disponibilite du WiFi. $_"
Write-Host ""
exit
}
# Tentative de connexion au réseau WiFi
try {
Write-Host ""
Write-Host "Connexion $wifiName en cours" -NoNewline
$cmd = "netsh wlan connect name=`"$wifiName`" interface=`"$interfaceName`""
$job = Start-Job -ScriptBlock { Invoke-Expression $using:cmd }
# Animation de connexion en cours
while ($job.State -eq 'Running') {
Write-Host "." -NoNewline
Start-Sleep -Seconds 1
}
Remove-Job $job
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de se connecter au WiFi. $_"
Write-Host ""
exit
}
# Vérifier le résultat de la connexion
try {
$connected = netsh wlan show interfaces | Select-String -Pattern $wifiName
if ($connected) {
Write-Host ""
Write-Host "Connexion $wifiName OK."
Write-Host ""
} else {
Write-Host ""
Write-Host "Erreur : Connexion $wifiName impossible."
Write-Host ""
}
} catch {
Write-Host ""
Write-Host "Erreur : Impossible de tester la connexion. $_"
Write-Host ""
}
# Nettoyer le fichier de profil WiFi
Remove-Item $profilePath