On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
BS = Chr(92) ' Backslash
DBS = BS & BS ' Double Backslash
Set wshNetwork = WScript.CreateObject("WScript.Network" )
Set wshShell = WScript.CreateObject("WScript.Shell" )
Set wshSysEnv = wshShell.Environment("SYSTEM" )
sUser = LCase(wshShell.ExpandEnvironmentStrings("%USERNAME%" ))
sComputer = LCase(wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%" ))
sServer = "nomduserveur"
' The pairs are "printers" and "default printer for computers"
aryPrinters = Array(Array(DBS & sServer & BS & "comptabilite_hp_laserjet_color_4600n", "pc001;pc002" ), _
Array(DBS & sServer & BS & "comptabilite_hp_laserjet_2420", "pc004" ), _
Array(DBS & sServer & BS & "couloir_hp_laserjet_4050n", "" ), _
Array(DBS & sServer & BS & "HPlaserjet 4250", "pc003;pc005;pc006;pc007;pc008" ), _
Array(DBS & sServer & BS & "couloir_hp_laserjet_5si", "" ), _
Array(DBS & sServer & BS & "couloir_photocopieuse_sharp", "" ), _
Array(DBS & sServer & BS & "couloir_hp_business_inkjet_2800_a3", "" ), _
Array(DBS & "autremachine" & BS & "valeur_hp_980cxi", "pc009;pc010" ))
' Disconnect all printers
Sub DisconnectPrinters()
Set colPrinters = wshNetwork.EnumPrinterConnections
For i = 0 To colPrinters.Count - 1 Step 2
wshNetwork.RemovePrinterConnection colPrinters(i)
Next
End Sub
' Map printer
Sub MapPrinter(sPrinter)
wshNetwork.AddWindowsPrinterConnection sPrinter
End Sub
' Set Default Printer
Sub SetDefaultPrinter(sPrinter)
wshNetwork.SetDefaultPrinter sPrinter
End Sub
DisconnectPrinters
' Connect printers
For i = LBound(aryPrinters) to UBound(aryPrinters)
aryAux = aryPrinters(i)
MapPrinter aryAux(0)
aryComputers = Split(aryAux(1), ";" )
For j = LBound(aryComputers) to UBound(aryComputers)
If sComputer = aryComputers(j) Then
SetDefaultPrinter(aryAux(0))
End If
Next
Next
' Delete objects
Set wshNetwork = Nothing
Set wshShell = Nothing
Set wshSysEnv = Nothing |