Ping | Script de vérification de la connexion
Rédigé par O. Clotaire
Aucun commentaire
Classé dans : Windows
Le Ping utilise une requête ICMP et attend une réponse pour confirmé l’accessibilité avec une autre machine. Ici, nous voyons deux méthodes. L'une avec un Batch et l'autre avec PowerShell.
Avec l'Invite de commandes
@ECHO OFF
Rem -----------------------------------
:: Affiche le nom de la fenetre désiré
TITLE Test la connexion %USERNAME%
ECHO Test la connexion...
Rem -----------------------------------
:: Ont Ping google.fr pendant 5secondes
PING www.google.fr -n 6 > NUL
Rem -----------------------------------
:: Si errorlevel vaut 0 alors tout et OK
IF %ERRORLEVEL% == 0 (
CLS
ECHO Informations :
ECHO Site - www.google.fr
ECHO Etats - [OK]
ECHO Internet - [OUI]
)
Rem -----------------------------------
:: Si errorlevel vaut 1 alors tout et NUL
IF %ERRORLEVEL% == 1 (
CLS
ECHO Informations :
ECHO Site - www.google.fr
ECHO Etats - [NUL]
ECHO Internet - [NON]
)
Rem -----------------------------------
:: Cache "Appuyer sur....pour continuer
PAUSE>NUL
Avec PowerShell
function get-onlinetest
{
Param
(
# Name of computer
$Computername
)
# fonction de test
try
{
$connetion = Test-Connection -computername $Computername -Count 1 -ErrorAction stop
}
catch [System.Net.NetworkInformation.PingException]
{
return "$Computername offline"
}
Write-Host "$Computername online"
}
#execution du code
$computers = "localhost","127.0.0.1","192.168.0.1"
foreach ($computer in $computers){get-onlinetest $computer}