Advertisement
barnabe0057

Imprimante réseau 2023

Feb 16th, 2023 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 13.90 KB | None | 0 0
  1. @echo off
  2. ver | find "10.0." >nul && chcp 65001 >nul || chcp 28591 >nul
  3. color 0A
  4. setlocal EnableExtensions EnableDelayedExpansion
  5. mode con: cols=90 lines=30
  6.  
  7. : -------------------------------------------------------------------------------------------------------------------------
  8. : on définit les variables
  9. : -------------------------------------------------------------------------------------------------------------------------
  10.  
  11. set "site=client2"
  12.  
  13. set "allowNetworkScan=0"
  14. set "driversStore=%~dp0Drivers Store"
  15. set "port=9100"
  16. set "nmapVersion=nmap-7.92"
  17.  
  18. title Installation d'imprimantes reseau - %site%
  19.  
  20. : -------------------------------------------------------------------------------------------------------------------------
  21. : on vérifie l'existence du dossier contenant tous les pilotes
  22. : -------------------------------------------------------------------------------------------------------------------------
  23.  
  24. cd /d %driversStore% || (
  25.     echo. & echo ^>^>^> Impossible de trouver le dossier des pilotes, le script va quitter
  26.     echo. & timeout /t 5 /nobreak >nul
  27.     exit /b 1
  28. )
  29.  
  30. : -------------------------------------------------------------------------------------------------------------------------
  31. : on détermine si le système est 32 ou 64 bits
  32. : -------------------------------------------------------------------------------------------------------------------------
  33.  
  34. wmic os get osarchitecture /value | find "64" >nul && set "arch=x64" || set "arch=x86"
  35.  
  36. : -------------------------------------------------------------------------------------------------------------------------
  37. : on recherche les imprimantes à installer
  38. : -------------------------------------------------------------------------------------------------------------------------
  39.  
  40. if not exist "%~dp0printers_list.csv" (
  41.     echo. & echo ^>^>^> Impossible de trouver la liste des imprimantes, le script va quitter
  42.     echo. & timeout /t 5 /nobreak >nul
  43.     echo #modèle;nom affiché;adresse mac;adresse ip;site;>"%~dp0printers_list.csv"
  44.     exit /b 1
  45. )
  46.  
  47. for /f "delims=" %%H in ('findstr "%site%" "%~dp0printers_list.csv"') do (
  48.    
  49.     call :filling "%%~H"
  50.    
  51.     for /f "eol=# tokens=1-4 delims=;" %%A in ('echo !line!') do (
  52.         set "printerModel=%%~A"
  53.         set "printerName=%%~B"
  54.         set "printerMacAddress=%%~C"
  55.         set "printerIpAddress=%%~D"
  56.  
  57.         set "printerModel=!printerModel:###=!"
  58.         set "printerName=!printerName:###=!"
  59.         set "printerMacAddress=!printerMacAddress:###=!"
  60.         set "printerIpAddress=!printerIpAddress:###=!"
  61.     )
  62.  
  63.     if defined printerModel (
  64.         if exist "%driversStore%\!printerModel!" (
  65.             pushd "%driversStore%\!printerModel!"
  66.             call :process
  67.             popd
  68.         )
  69.     )
  70.     timeout /t 5 /nobreak
  71. )
  72.  
  73. exit /b 0
  74.  
  75. : -------------------------------------------------------------------------------------------------------------------------
  76. : début de la routine d'installation
  77. : -------------------------------------------------------------------------------------------------------------------------
  78.  
  79. :process
  80.  
  81. : -------------------------------------------------------------------------------------------------------------------------
  82. : on vérifie si l'imprimante n'est pas déjà installée
  83. : -------------------------------------------------------------------------------------------------------------------------
  84.  
  85. wmic printer get Name,DriverName /format:table | find /i "!printerModel!" >nul && (
  86.     echo. & echo ^>^>^> L'imprimante !printerModel! est déjà installée sur le poste de travail
  87.     echo.
  88.     exit /b 1
  89. )
  90.  
  91. : -------------------------------------------------------------------------------------------------------------------------
  92. : on recherche le script d'installation (fichier .inf) du pilote de l'imprimante
  93. : -------------------------------------------------------------------------------------------------------------------------
  94.  
  95. set "infPrint="
  96.  
  97. cls
  98. echo.
  99. echo ######################################################
  100. echo Etape 1 - Recherche du pilote d'impression
  101. echo ######################################################
  102.  
  103. rem on recherche dans les fichiers .inf correspondants à l'architecture
  104. for %%A in (*%arch%*.inf) do (
  105.     find /i "Class" "%%~A" | find /i "Printer" >nul && if not defined infPrint (call :extraction "%%~A" infPrint || exit /b 1)
  106.     find /i "Class" "%%~A" | find /i "Image" >nul && if not defined infScan (call :extraction "%%~A" infScan || exit /b 1)
  107. )
  108.  
  109. rem on recherche dans tous les fichiers .inf
  110. for %%A in (*.inf) do (
  111.     find /i "Class" "%%~A" | find /i "Printer" >nul && if not defined infPrint (call :extraction "%%~A" infPrint || exit /b 1)
  112.     find /i "Class" "%%~A" | find /i "Image" >nul && if not defined infScan (call :extraction "%%~A" infScan || exit /b 1)
  113. )
  114.  
  115. if not defined infPrint (
  116.     echo.
  117.     echo ^>^>^> Aucun script d'installation n'a été trouvé pour l'imprimante '!printerModel!'
  118.     echo.
  119.     exit /b 1
  120. )
  121.  
  122. : -------------------------------------------------------------------------------------------------------------------------
  123. : on lance l'installation si l'adresse ip a été renseignée au début du script, sinon on fait appel à Nmap
  124. : -------------------------------------------------------------------------------------------------------------------------
  125.  
  126. timeout /t 3 /nobreak >nul
  127.  
  128. if defined printerIpAddress (
  129.     call :installation "!printerIpAddress!"
  130.     exit /b 0
  131. )
  132.  
  133. if "%allowNetworkScan%"=="0" (exit /b 1)
  134.  
  135. : -------------------------------------------------------------------------------------------------------------------------
  136. : on vérifie les prérequis pour le téléchargement et l'extraction du programme Nmap
  137. : -------------------------------------------------------------------------------------------------------------------------
  138.  
  139. if not defined printerMacAddress (
  140.     echo. & echo ^>^>^> Vous avez omis d'indiquer au début du script l'adresse MAC de l'imprimante
  141.     echo. & timeout /t 5 /nobreak >nul
  142.     exit /b 1
  143. )
  144.  
  145. 2>nul call 7za | find "Pavlov" >nul || (
  146.     echo. & echo ^>^>^> L'outil 7za est manquant, il est disponible ici : https://www.7-zip.fr/download.html
  147.     echo. & timeout /t 5 /nobreak >nul
  148.     exit /b 1
  149. )
  150.  
  151. 2>nul call curl -V | find "libcurl" >nul || (
  152.     echo. & echo ^>^>^> L'outil curl est manquant, il est disponible ici : https://curl.se/download.html
  153.     echo. & timeout /t 5 /nobreak >nul
  154.     exit /b 1
  155. )
  156.  
  157. : -------------------------------------------------------------------------------------------------------------------------
  158. : on télécharge le scanner réseau NMAP
  159. : -------------------------------------------------------------------------------------------------------------------------
  160.  
  161. set "PATH=%TMP%\%nmapVersion%;%PATH%"
  162.  
  163. pushd %TMP%
  164.  
  165. nmap -V >nul 2>&1 || (
  166.  
  167.     echo ######################################################
  168.     echo Etape requise - Téléchargement de l'outil NMAP
  169.     echo ######################################################
  170.     echo.
  171.     curl -# -k -o "%nmapVersion%-win32.zip" "https://nmap.org/dist/%nmapVersion%-win32.zip"
  172.     cls & echo.
  173.     echo ######################################################
  174.     echo Etape requise - Décompression de l'outil NMAP
  175.     echo ######################################################
  176.     7za x "%nmapVersion%-win32.zip" -y
  177.  
  178. )
  179.  
  180. popd
  181.  
  182. : -------------------------------------------------------------------------------------------------------------------------
  183. : on vérifie si NpCap est installé
  184. : -------------------------------------------------------------------------------------------------------------------------
  185.  
  186. :verif
  187.  
  188. nmap -V 2>&1 | find "WARNING:" >nul && (
  189.     echo. & echo ### Veuillez d'abord installer le programme NpCap
  190.     for %%A in ("%TMP%\%nmapVersion%\npcap*.exe") do set "npcap=%%~A"
  191.     echo. & pause
  192.     start "" /D "%TMP%\%nmapVersion%" /WAIT "%npcap%"
  193.     goto :verif
  194. )
  195.  
  196. : -------------------------------------------------------------------------------------------------------------------------
  197. : on scanne le réseau avec l'outil NMAP
  198. : -------------------------------------------------------------------------------------------------------------------------
  199.  
  200. 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")
  201. set "network=!network: =!"
  202.  
  203. set "deviceMacAddress="
  204.  
  205. cls & echo.
  206. echo ######################################################
  207. echo Etape 2 - Recherche de l'IP imprimante
  208. echo ######################################################
  209. echo.
  210.  
  211. set "log=%TMP%\report_nmap.txt"
  212. nmap.exe -n -oN %log% --open -p T:%port% !network!
  213.  
  214. timeout /t 2 /nobreak >nul
  215.  
  216. : -------------------------------------------------------------------------------------------------------------------------
  217. : on analyse le fichier journal pour extraire l'ip de l'imprimante concernée
  218. : -------------------------------------------------------------------------------------------------------------------------
  219.  
  220. for /f "tokens=5" %%M in ('findstr /i /C:"Nmap scan report for" "%log%"') do set "printerIpAddress=%%M"
  221. for /f "tokens=3-8 delims=:( " %%P in ('findstr /i /C:"MAC Address" "%log%"') do set "deviceMacAddress=%%P:%%Q:%%R:%%S:%%T:%%U"
  222.  
  223. if "!deviceMacAddress!"=="!printerMacAddress!" (call :installation "!printerIpAddress!") else (echo. & echo ^>^>^> L'imprimante ciblée est éteinte/inaccessible)
  224.  
  225. exit /b 0
  226.  
  227. : -------------------------------------------------------------------------------------------------------------------------
  228. : on crée un port TCP/IP contenant l'ip de l'imprimante
  229. : -------------------------------------------------------------------------------------------------------------------------
  230.  
  231. :installation
  232.  
  233. cls & echo.
  234. echo ^>^>^>^>^> !printerModel! = [ %~1 ] ^<^<^<^<^<
  235. echo. & echo.
  236. echo ######################################################
  237. echo Etape 3 - Création du port d'impression TCP/IP
  238. echo ######################################################
  239. echo.
  240.  
  241. for /f "tokens=1 delims={}" %%A in ('wmic os get MUILanguages ^| find "-"') do if not defined locale (set "locale=%%~A")
  242.  
  243. cscript "%WINDIR%\System32\Printing_Admin_Scripts\%locale%\prnport.vbs" -a -r "IP_%~1" -h %~1 -o raw -n %port%
  244.  
  245. : -------------------------------------------------------------------------------------------------------------------------
  246. : on ajoute/installe l'imprimante dans le système
  247. : -------------------------------------------------------------------------------------------------------------------------
  248.  
  249. echo. & echo.
  250. echo ######################################################
  251. echo Etape 4 - Ajout de l'imprimante réseau
  252. echo ######################################################
  253. echo.
  254.  
  255. if not defined printerName (set "printerName=!printerModel!")
  256.  
  257. rundll32 printui.dll,PrintUIEntry /if /b "!printerName!" /f "!infPrint!" /r "IP_%~1" /h %arch% /m "!printerDriverName!" /z
  258. IF %ERRORLEVEL% NEQ 0 (set "result=NOK") else (set "result=OK")
  259. echo ^=^=^>^> ajout imprimante : !result!
  260. call :logging !result!
  261.  
  262. rundll32 printui.dll,PrintUIEntry /y /n "!printerName!"
  263. IF %ERRORLEVEL% NEQ 0 (set "result=NOK") else (set "result=OK")
  264. echo. & echo ^=^=^>^> imprimante par défaut : !result!
  265.  
  266. exit /b 0
  267.  
  268. : -------------------------------------------------------------------------------------------------------------------------
  269. : on récupère le nom du pilote contenu dans le .inf
  270. : -------------------------------------------------------------------------------------------------------------------------
  271.  
  272. :extraction
  273.  
  274. set "driverName="
  275. for /f "skip=2 tokens=1,2 delims=,=""" %%E in ('find "!printerModel!" "%~1"') do (
  276.     if not defined driverName (echo %%~E | findstr "Printer series Series LAN" >nul && set "driverName=%%~E")
  277. )
  278. if not defined driverName (pause & exit)
  279.  
  280. :supEspace
  281. if defined driverName (
  282.     if "!driverName:~-1!"==" " (
  283.         set "driverName=!driverName:~0,-1!"
  284.         goto :supEspace
  285.     )
  286.     set "driverName=!driverName:"=!"
  287.     set "%2=%~1"
  288. ) else (exit /b 1)
  289.  
  290. if "%2"=="infPrint" (set "printerDriverName=!driverName!")
  291. if "%2"=="infScan" (set "scannerDriverName=!driverName!")
  292.  
  293. : -------------------------------------------------------------------------------------------------------------------------
  294. : on affiche les informations issues du fichier .inf
  295. : -------------------------------------------------------------------------------------------------------------------------
  296.  
  297. for /f "tokens=2,3 delims=,=" %%A in ('find /i "DriverVer" "%~1"') do set "version=%%B" & set "creation=%%A"
  298.  
  299. echo.
  300. echo ^>^>^> Modèle        : '!printerModel!'
  301. echo ^>^>^> Fichier INF   : '%~1'
  302. echo ^>^>^> Nom du pilote : '!driverName!'
  303. echo.
  304. echo ^>^>^> Script d'installation : OK
  305.  
  306. set "inf=%~1"
  307. if exist "!inf:inf=cat!" (echo ^>^>^> Catalogue de sécurité : OK) else (exit /b 1)
  308.  
  309. echo.
  310. echo ^>^>^> Version du pilote     : !version: =!
  311. echo ^>^>^> Date du pilote        : !creation: =!
  312. echo.
  313. echo ######################################################
  314.  
  315. goto :eof
  316.  
  317. : -------------------------------------------------------------------------------------------------------------------------
  318. : on remplit les champs vides avec un motif
  319. : -------------------------------------------------------------------------------------------------------------------------
  320.  
  321. :filling
  322.  
  323. rem echo $ %~1
  324. set "line=%~1"
  325.  
  326. :boucle
  327. set "line=!line:;;=;###;!"
  328. echo.!line! | findstr ";;" >nul && goto :boucle
  329.  
  330. goto :eof
  331.  
  332. : -------------------------------------------------------------------------------------------------------------------------
  333. : routine permettant la journalisation
  334. : -------------------------------------------------------------------------------------------------------------------------
  335.  
  336. :logging
  337.  
  338. if not exist "%~dp0Log" (mkdir "%~dp0Log")
  339.  
  340. for /f "tokens=1,2,3 delims=/ " %%a in ('date /t') do set "maDate=%%a-%%b-%%c"
  341. for /f "tokens=1,2 delims=:" %%a in ('time /t') do set "heure=%%a:%%b:%time:~6,2%"
  342.  
  343. (echo.
  344. echo ^>^>^>^>^> date : !maDate! à !heure!
  345. echo ^>^>^>^>^> ajout imprimante "!printerModel!" : !REG3XP0!>%~1
  346. echo ^>^>^>^>^> nom de l'ordinateur : %COMPUTERNAME%)>>"%~dp0Log\%site%.txt"
  347.  
  348. goto :eof
  349.  
  350. rem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement