Migrer des imprimantes réseau à l’aide d’un script VBS

Voici une de mes réalisations dans le but de reconnecter facilement toutes les imprimantes réseaux sur des postes clients suite à un changement du plan d’adressage.

Le site Activexperts m’a pas mal aidé dans le développement de ce script VBS.

Je tiens à préciser qu’il faut être administrateur sur l’ordinateur pour pouvoir l’exécuter.

Le script a pour but de recréer un port TCP/IP local s’il trouve une concordance avec une imprimante listée dans la collection en début de script. Ainsi par exemple, si un port TCP/IP local utilise l’adresse IP 10.0.1.10, elle deviendra 192.168.1.100. Pour fonctionner, le port doit porter un nom ressemblant à « IP_10.0.1.10 » ou juste « 10.0.1.10 » sous peine de se voir exclu.

La procédure est la suivante et s’effectuera automatiquement pour chaque imprimante trouvée :

  • recherche de l’imprimante au moyen de son nom de port actuel,
  • création d’un nouveau port TCP/IP local,
  • mappage de l’imprimante sur le nouveau port,
  • suppression de l’ancien port.
'
' Script VBS permettant la reconnexion des imprimantes réseaux
' d'un ordinateur suite à un changement du plan d'adressage.
' Il faudra exécuter ce script avec une élévation de privilèges
' sous peine de se voir refuser l'accès.
'
' https://blog.lumo.fr/migrer-des-imprimantes-reseau-a-laide-dun-script-vbs.html
'

' Constants
strComputer="."
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

' Variants
Dim myPrinters(5,1)

' Filling the collection
myPrinters(0,0) = "10.0.1.10"
myPrinters(0,1) = "192.168.1.100"
myPrinters(1,0) = "10.0.1.11"
myPrinters(1,1) = "192.168.1.101"
myPrinters(2,0) = "10.0.1.12"
myPrinters(2,1) = "192.168.1.102"

' Initiate WMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")

' Get collection of local printers with an IP address in the current range
' PortName can contain the string "IP_" or not
Set getPrinters = objWMI.ExecQuery("SELECT * FROM Win32_Printer WHERE Local=True AND (PortName LIKE 'IP_192.33.50.%' OR PortName LIKE '192.33.50.%')", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

' Iterate through that collection
For Each objPrinter In getPrinters
	' Display the details for each printer
	WScript.Echo "Printer Name = " & objPrinter.Name
	WScript.Echo "Current Port = " & objPrinter.PortName
	' Replace the current port name after a search into the collection myPrinters
	For aPrinter = 0 To 5
		' Deleting the string "IP_" if exists
		tempPortName = Replace(objPrinter.PortName, "IP_", "")
		' Comparaison
		If (StrComp(tempPortName, myPrinters(aPrinter,0))) = 0 Then
			Set getPorts = objWMI.ExecQuery("SELECT * FROM Win32_TCPIPPrinterPort WHERE HostAddress='" & myPrinters(aPrinter,1) & "'")
			If getPorts.Count = 1 Then
				WScript.Echo "New port already exists"
			Else
				' Creating new port
				WScript.Echo "Creating new port ..."
				Set objNewPort = objWMI.Get("Win32_TCPIPPrinterPort").SpawnInstance_
				objNewPort.Name = "IP_" & myPrinters(aPrinter,1)
				objNewPort.Protocol = 1
				objNewPort.HostAddress = myPrinters(aPrinter,1)
				objNewPort.Put_
			End If
			' Now map the printer to the new port
			WScript.Echo "Mapping this printer to the new port" & VBCrLf
			objPrinter.PortName = "IP_" & myPrinters(aPrinter,1)
			objPrinter.Put_
			' Now, the old port is not in use, we can delete it
			WScript.Echo "Deleting old port ..."
			Set getPortsToDelete = objWMI.ExecQuery("SELECT * FROM Win32_TCPIPPrinterPort WHERE HostAddress='" & myPrinters(aPrinter,0) & "'")
			For Each portToDelete In getPortsToDelete
				portToDelete.Delete_
			Next
		End If
	Next
Next