barnabe0057 icon

Imprimante réseau 2023

barnabe0057 | PRO | 10/23/23 08:06:52 PM UTC (Edited) | 0 ⭐ | 4182 👁️ | Never ⏰ | []
Batch |

13.9 KB

|

None

|

0 👍

/

0 👎

@echo off
ver | find "10.0." >nul && chcp 65001 >nul || chcp 28591 >nul
color 0A
setlocal EnableExtensions EnableDelayedExpansion
mode con: cols=90 lines=30
 
: -------------------------------------------------------------------------------------------------------------------------
: on définit les variables
: -------------------------------------------------------------------------------------------------------------------------
 
set "site=client2"
 
set "allowNetworkScan=0"
set "driversStore=%~dp0Drivers Store"
set "port=9100"
set "nmapVersion=nmap-7.92"
 
title Installation d'imprimantes reseau - %site%
 
: -------------------------------------------------------------------------------------------------------------------------
: on vérifie l'existence du dossier contenant tous les pilotes
: -------------------------------------------------------------------------------------------------------------------------
 
cd /d %driversStore% || (
    echo. & echo ^>^>^> Impossible de trouver le dossier des pilotes, le script va quitter
    echo. & timeout /t 5 /nobreak >nul
    exit /b 1
)
 
: -------------------------------------------------------------------------------------------------------------------------
: on détermine si le système est 32 ou 64 bits
: -------------------------------------------------------------------------------------------------------------------------
 
wmic os get osarchitecture /value | find "64" >nul && set "arch=x64" || set "arch=x86"
 
: -------------------------------------------------------------------------------------------------------------------------
: on recherche les imprimantes à installer
: -------------------------------------------------------------------------------------------------------------------------
 
if not exist "%~dp0printers_list.csv" (
    echo. & echo ^>^>^> Impossible de trouver la liste des imprimantes, le script va quitter
    echo. & timeout /t 5 /nobreak >nul
    echo #modèle;nom affiché;adresse mac;adresse ip;site;>"%~dp0printers_list.csv"
    exit /b 1
)
 
for /f "delims=" %%H in ('findstr "%site%" "%~dp0printers_list.csv"') do (
    
    call :filling "%%~H"
    
    for /f "eol=# tokens=1-4 delims=;" %%A in ('echo !line!') do (
        set "printerModel=%%~A"
        set "printerName=%%~B"
        set "printerMacAddress=%%~C"
        set "printerIpAddress=%%~D"
 
        set "printerModel=!printerModel:###=!"
        set "printerName=!printerName:###=!"
        set "printerMacAddress=!printerMacAddress:###=!"
        set "printerIpAddress=!printerIpAddress:###=!"
    )
 
    if defined printerModel (
        if exist "%driversStore%\!printerModel!" (
            pushd "%driversStore%\!printerModel!"
            call :process
            popd
        )
    )
    timeout /t 5 /nobreak
)
 
exit /b 0
 
: -------------------------------------------------------------------------------------------------------------------------
: début de la routine d'installation
: -------------------------------------------------------------------------------------------------------------------------
 
:process
 
: -------------------------------------------------------------------------------------------------------------------------
: on vérifie si l'imprimante n'est pas déjà installée
: -------------------------------------------------------------------------------------------------------------------------
 
wmic printer get Name,DriverName /format:table | find /i "!printerModel!" >nul && (
    echo. & echo ^>^>^> L'imprimante !printerModel! est déjà installée sur le poste de travail
    echo.
    exit /b 1
)
 
: -------------------------------------------------------------------------------------------------------------------------
: on recherche le script d'installation (fichier .inf) du pilote de l'imprimante
: -------------------------------------------------------------------------------------------------------------------------
 
set "infPrint="
 
cls
echo.
echo ######################################################
echo Etape 1 - Recherche du pilote d'impression
echo ######################################################
 
rem on recherche dans les fichiers .inf correspondants à l'architecture
for %%A in (*%arch%*.inf) do (
    find /i "Class" "%%~A" | find /i "Printer" >nul && if not defined infPrint (call :extraction "%%~A" infPrint || exit /b 1)
    find /i "Class" "%%~A" | find /i "Image" >nul && if not defined infScan (call :extraction "%%~A" infScan || exit /b 1)
)
 
rem on recherche dans tous les fichiers .inf
for %%A in (*.inf) do (
    find /i "Class" "%%~A" | find /i "Printer" >nul && if not defined infPrint (call :extraction "%%~A" infPrint || exit /b 1)
    find /i "Class" "%%~A" | find /i "Image" >nul && if not defined infScan (call :extraction "%%~A" infScan || exit /b 1)
)
 
if not defined infPrint (
    echo.
    echo ^>^>^> Aucun script d'installation n'a été trouvé pour l'imprimante '!printerModel!'
    echo.
    exit /b 1
)
 
: -------------------------------------------------------------------------------------------------------------------------
: on lance l'installation si l'adresse ip a été renseignée au début du script, sinon on fait appel à Nmap
: -------------------------------------------------------------------------------------------------------------------------
 
timeout /t 3 /nobreak >nul
 
if defined printerIpAddress (
    call :installation "!printerIpAddress!"
    exit /b 0
)
 
if "%allowNetworkScan%"=="0" (exit /b 1)
 
: -------------------------------------------------------------------------------------------------------------------------
: on vérifie les prérequis pour le téléchargement et l'extraction du programme Nmap
: -------------------------------------------------------------------------------------------------------------------------
 
if not defined printerMacAddress (
    echo. & echo ^>^>^> Vous avez omis d'indiquer au début du script l'adresse MAC de l'imprimante
    echo. & timeout /t 5 /nobreak >nul
    exit /b 1
)
 
2>nul call 7za | find "Pavlov" >nul || (
    echo. & echo ^>^>^> L'outil 7za est manquant, il est disponible ici : https://www.7-zip.fr/download.html
    echo. & timeout /t 5 /nobreak >nul
    exit /b 1
)
 
2>nul call curl -V | find "libcurl" >nul || (
    echo. & echo ^>^>^> L'outil curl est manquant, il est disponible ici : https://curl.se/download.html
    echo. & timeout /t 5 /nobreak >nul
    exit /b 1
)
 
: -------------------------------------------------------------------------------------------------------------------------
: on télécharge le scanner réseau NMAP
: -------------------------------------------------------------------------------------------------------------------------
 
set "PATH=%TMP%\%nmapVersion%;%PATH%"
 
pushd %TMP%
 
nmap -V >nul 2>&1 || (
 
    echo ######################################################
    echo Etape requise - Téléchargement de l'outil NMAP
    echo ######################################################
    echo.
    curl -# -k -o "%nmapVersion%-win32.zip" "https://nmap.org/dist/%nmapVersion%-win32.zip"
    cls & echo.
    echo ######################################################
    echo Etape requise - Décompression de l'outil NMAP
    echo ######################################################
    7za x "%nmapVersion%-win32.zip" -y
 
)
 
popd
 
: -------------------------------------------------------------------------------------------------------------------------
: on vérifie si NpCap est installé
: -------------------------------------------------------------------------------------------------------------------------
 
:verif
 
nmap -V 2>&1 | find "WARNING:" >nul && (
    echo. & echo ### Veuillez d'abord installer le programme NpCap
    for %%A in ("%TMP%\%nmapVersion%\npcap*.exe") do set "npcap=%%~A"
    echo. & pause
    start "" /D "%TMP%\%nmapVersion%" /WAIT "%npcap%"
    goto :verif
)
 
: -------------------------------------------------------------------------------------------------------------------------
: on scanne le réseau avec l'outil NMAP
: -------------------------------------------------------------------------------------------------------------------------
 
for /f "tokens=2 delims=:(" %%A in ('netsh interface ip show address ^| find "/" ^| findstr /v "127.0."') do if not defined network (set "network=%%~A")
set "network=!network: =!"
 
set "deviceMacAddress="
 
cls & echo.
echo ######################################################
echo Etape 2 - Recherche de l'IP imprimante
echo ######################################################
echo.
 
set "log=%TMP%\report_nmap.txt"
nmap.exe -n -oN %log% --open -p T:%port% !network!
 
timeout /t 2 /nobreak >nul
 
: -------------------------------------------------------------------------------------------------------------------------
: on analyse le fichier journal pour extraire l'ip de l'imprimante concernée
: -------------------------------------------------------------------------------------------------------------------------
 
for /f "tokens=5" %%M in ('findstr /i /C:"Nmap scan report for" "%log%"') do set "printerIpAddress=%%M"
for /f "tokens=3-8 delims=:( " %%P in ('findstr /i /C:"MAC Address" "%log%"') do set "deviceMacAddress=%%P:%%Q:%%R:%%S:%%T:%%U"
 
if "!deviceMacAddress!"=="!printerMacAddress!" (call :installation "!printerIpAddress!") else (echo. & echo ^>^>^> L'imprimante ciblée est éteinte/inaccessible)
 
exit /b 0
 
: -------------------------------------------------------------------------------------------------------------------------
: on crée un port TCP/IP contenant l'ip de l'imprimante
: -------------------------------------------------------------------------------------------------------------------------
 
:installation
 
cls & echo.
echo ^>^>^>^>^> !printerModel! = [ %~1 ] ^<^<^<^<^<
echo. & echo.
echo ######################################################
echo Etape 3 - Création du port d'impression TCP/IP
echo ######################################################
echo.
 
for /f "tokens=1 delims={}" %%A in ('wmic os get MUILanguages ^| find "-"') do if not defined locale (set "locale=%%~A")
 
cscript "%WINDIR%\System32\Printing_Admin_Scripts\%locale%\prnport.vbs" -a -r "IP_%~1" -h %~1 -o raw -n %port%
 
: -------------------------------------------------------------------------------------------------------------------------
: on ajoute/installe l'imprimante dans le système
: -------------------------------------------------------------------------------------------------------------------------
 
echo. & echo.
echo ######################################################
echo Etape 4 - Ajout de l'imprimante réseau
echo ######################################################
echo.
 
if not defined printerName (set "printerName=!printerModel!")
 
rundll32 printui.dll,PrintUIEntry /if /b "!printerName!" /f "!infPrint!" /r "IP_%~1" /h %arch% /m "!printerDriverName!" /z
IF %ERRORLEVEL% NEQ 0 (set "result=NOK") else (set "result=OK")
echo ^=^=^>^> ajout imprimante : !result!
call :logging !result!
 
rundll32 printui.dll,PrintUIEntry /y /n "!printerName!"
IF %ERRORLEVEL% NEQ 0 (set "result=NOK") else (set "result=OK")
echo. & echo ^=^=^>^> imprimante par défaut : !result!
 
exit /b 0
 
: -------------------------------------------------------------------------------------------------------------------------
: on récupère le nom du pilote contenu dans le .inf
: -------------------------------------------------------------------------------------------------------------------------
 
:extraction
 
set "driverName="
for /f "skip=2 tokens=1,2 delims=,=""" %%E in ('find "!printerModel!" "%~1"') do (
    if not defined driverName (echo %%~E | findstr "Printer series Series LAN" >nul && set "driverName=%%~E")
)
if not defined driverName (pause & exit)
 
:supEspace
if defined driverName (
    if "!driverName:~-1!"==" " (
        set "driverName=!driverName:~0,-1!"
        goto :supEspace
    )
    set "driverName=!driverName:"=!"
    set "%2=%~1"
) else (exit /b 1)
 
if "%2"=="infPrint" (set "printerDriverName=!driverName!")
if "%2"=="infScan" (set "scannerDriverName=!driverName!")
 
: -------------------------------------------------------------------------------------------------------------------------
: on affiche les informations issues du fichier .inf
: -------------------------------------------------------------------------------------------------------------------------
 
for /f "tokens=2,3 delims=,=" %%A in ('find /i "DriverVer" "%~1"') do set "version=%%B" & set "creation=%%A"
 
echo.
echo ^>^>^> Modèle        : '!printerModel!'
echo ^>^>^> Fichier INF   : '%~1'
echo ^>^>^> Nom du pilote : '!driverName!'
echo.
echo ^>^>^> Script d'installation : OK
 
set "inf=%~1"
if exist "!inf:inf=cat!" (echo ^>^>^> Catalogue de sécurité : OK) else (exit /b 1)
 
echo.
echo ^>^>^> Version du pilote     : !version: =!
echo ^>^>^> Date du pilote        : !creation: =!
echo.
echo ######################################################
 
goto :eof
 
: -------------------------------------------------------------------------------------------------------------------------
: on remplit les champs vides avec un motif
: -------------------------------------------------------------------------------------------------------------------------
 
:filling
 
rem echo $ %~1
set "line=%~1"
 
:boucle
set "line=!line:;;=;###;!"
echo.!line! | findstr ";;" >nul && goto :boucle
 
goto :eof
 
: -------------------------------------------------------------------------------------------------------------------------
: routine permettant la journalisation
: -------------------------------------------------------------------------------------------------------------------------
 
:logging
 
if not exist "%~dp0Log" (mkdir "%~dp0Log")
 
for /f "tokens=1,2,3 delims=/ " %%a in ('date /t') do set "maDate=%%a-%%b-%%c"
for /f "tokens=1,2 delims=:" %%a in ('time /t') do set "heure=%%a:%%b:%time:~6,2%"
 
(echo.
echo ^>^>^>^>^> date : !maDate! à !heure!
echo ^>^>^>^>^> ajout imprimante "!printerModel!" : !REG3XP0!>%~1
echo ^>^>^>^>^> nom de l'ordinateur : %COMPUTERNAME%)>>"%~dp0Log\%site%.txt"
 
goto :eof
 
rem

Comments