##Script de ouf crée par Bastien Schwartz Clear-Host #on déclare les variables #Requires -Version 3.0 write-output "Version 3.0 de Powershell Minimum requis" $version="Launcher 1.1" $Serveurs=0 $Serveurf= 0 $t_ms=0 $i=0 $i2=0 $server=0 $response=0 $1=0 $2=0 [Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") function f_vpnopen { $Serveurs= Import-Csv .\config.csv $Serveurf= 0 $Serveurs2=0 $t_ms=@() $i=0 $i2=0 $server=0 $response=0 function f_vpn ($1, $2) # fonction qui va lancer stunnel + openvpn par rapport au variable $1 et $2 { clear-host Start-Process -FilePath ".\stunnel\stunnel.exe" -argumentlist "$1" -verb runAs Start-Process -FilePath ".\OpenVPN\OpenVPNPortable.exe" -argumentlist "--connect $2" -verb runAs } function f_ip ($Serveurf) #Fonction qui va servir à pinger les serveurs { $test = (Test-Connection -ComputerName $Serveurf -Count 2 | measure-Object -Property ResponseTime -Average).average $response = ($test -as [int] ) return $response } foreach ($server in $serveurs) # Boucle qui va lancer la fonction de ping pour chaque serveur dans le fichier csv et integrer la moyenne du ping dans un tableau { [int[]]$t_ms+= f_ip $server.server } [int]$i=$t_ms | Measure-Object -minimum | Select-Object -ExpandProperty Minimum #mesure du ping le plus bas dans le tableau [int]$i2= $t_ms.IndexOf($i) #on cherche l'index du ping le plus bas dans le tableau $Serveurs2= Import-Csv .\config.csv | Where-Object { $_.id -eq $i2} #on cherche la ligne du serveur avec le ping le plus bas par rapport a l'index trouver précédement f_vpn $Serveurs2.config1 $Serveurs2.config2 #puis on lance la fonction pour lancer les logiciels avec les parametres serveurs } function f_vpnclose #fonction qui va fermer la connexion { Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList '-command Get-Process openvpn* | Stop-Process -Force' Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList '-command Get-Process stunnel* | Stop-Process -Force'#on lance un powershell en admin pour couper openvpn } Add-Type -AssemblyName System.Windows.Forms $form = New-Object Windows.Forms.Form $form.Size = New-Object Drawing.Size @(310,120) $form.StartPosition = "CenterScreen" $form.Text = "$version" $Form.Opacity = 0.7 $btn = New-Object System.Windows.Forms.Button $btn.add_click({f_vpnopen}) $btn.Text = "Lancer le VPN" $btn.Size = New-Object Drawing.Size @(100,30) $btn.Location = New-Object System.Drawing.Point(0, 5) $btn2 = New-Object System.Windows.Forms.Button $btn2.Location = New-Object System.Drawing.Point(125, 5) $btn2.add_click({f_vpnclose}) $btn2.Text = "Couper le VPN" $btn2.Size = New-Object Drawing.Size @(100,30) $btn3 = New-Object System.Windows.Forms.Button $btn3.Location = New-Object System.Drawing.Point(250, 5) $btn3.add_click({[environment]::exit(0)}) $btn3.Text = "EXIT" $btn3.Size = New-Object Drawing.Size @(40,30) $form.Controls.Add($btn) $form.Controls.Add($btn2) $form.Controls.Add($btn3) $drc = $form.ShowDialog()