VbScript para duplex en impresora y predeterminarla

Hace tiempo que el mundo lucha por un espacio mas verde.

“Todos” reciclamos, contaminamos menos etc…

Flor

La empresa por la que trabajo en la actualizad, no es menos. Por esto, entre otros motivos, me he visto envuelto en una tarea de configurar de forma automática todos los ordenadores (hay un spool).

La solución era;

Configurar el duplex en la impresora del spool.

Eliminar la impresora del usuario

Conectar de nuevo la impresora del usuario (se establecen las configuración que hay en el spool)

Establecer la impresora predeterminada.

Para realizarlo nada mejor que un VbScript por Active Directory.

No hace nada mas que sacar un listado de las impresoras de red, las guarda en un fichero (con el nombre de red Ej: \spoolprinter1) Guardar en otro fichero la impresora predeterminada (de la misma manera, con un fichero) y  luego conectarlas todas de nuevo.

Aquí tenemos el script:

Const ForAppending = 8
Const ForReading = 1

‘ Get the local computer name.
strComputer = “.”
Dim aqui

‘ MsgBox “Printers have been mapped.” & vbCRLF & varDelText & vbCRLF & vbCRLF & “Impresoras Mapeadas”, 64, “Script Complete”
‘strComputer = “.”
‘Set objWMIService = GetObject(“winmgmts:” _
‘ & “{impersonationLevel=impersonate}!\” _
‘ & strComputer & “rootcimv2”)
‘Set colInstalledPrinters = objWMIService.ExecQuery _
‘ (“Select * from Win32_PrinterConfiguration “)
‘For Each objPrinter In colInstalledPrinters
‘ objPrinter.Duplex = False

‘Wscript.Echo “Duplex: ” & objPrinter.Duplex & objPrinter.Name
‘Next

Set oShell = CreateObject(“Wscript.Shell”)
strUserProfile = oShell.ExpandEnvironmentStrings(“%USERPROFILE%”)
Set filesys = CreateObject(“Scripting.FileSystemObject”)
dim filesys, filetxt, getname, path, demofile, demofile3, filesys3

path = filesys.GetAbsolutePathName(strUserProfile & “text.txt”)
getname = filesys.GetFileName(path)

If filesys.FileExists(path) Then
Set filesys= CreateObject(“Scripting.FileSystemObject”)
set demofile = filesys.GetFile(strUserProfile & “text.txt”)
demofile.Delete
End If

path3 = filesys.GetAbsolutePathName(strUserProfile & “text2.txt”)
getname = filesys.GetFileName(path3)
If filesys.FileExists(path3) Then

Set filesys3= CreateObject(“Scripting.FileSystemObject”)
set demofile3 = filesys3.GetFile(strUserProfile & “text2.txt”)
demofile3.Delete
End If

Set WshNetwork = CreateObject(“WScript.Network”)
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
Set colInstalledPrinters = objWMIService.ExecQuery(“Select * from Win32_Printer”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”,,48)
Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
filOutput = strUserProfile & “text.txt”
filOutput2 = strUserProfile & “text2.txt”

‘ Creates a text file with the listing of all network printers for the logged in user.
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
Set objOutputFile2 = objFSO.OpenTextFile (filOutput2, ForAppending, True)
For Each objPrinter in colInstalledPrinters
strTest = Left(objPrinter.Name, 2)
If strTest = “\” Then
If objPrinter.Name = “\Gestetner09P9CORMFP” then
‘MsgBox “AQUESTA NO” & objPrinter.Name
elseif objPrinter.Name = “\Gestetner09P9CORMFPFACT” then
‘MsgBox “AQUESTA NO” & objPrinter.Name
else
‘MsgBox objPrinter.Name
objOutputFile.WriteLine(objPrinter.Name)
WshNetwork.RemovePrinterConnection objPrinter.Name
End If

If objPrinter.Default = True Then

aqui = objPrinter.Name
objOutputFile2.WriteLine(objPrinter.Name)
‘MsgBox aqui
‘MsgBox objPrinter.Name & “DEFAULT”

End If

End If
Next
objOutputFile.Close
‘MsgBox “Backup Echo ‘” & filOutput & “‘.”, 64, “Script Complete”

‘ Opens the text file matching the logged in user and reconnects to the printers listed.
Set objTextFile = objFSO.OpenTextFile (filOutput, ForReading)

‘Set objTextFile2 = objFSO.OpenTextFile (filOutput2, ForReading)
‘ Do Until objTextFile2.AtEndOfStream
‘ strPrinter = objTextFile2.Readline
‘ strTest = Left(strPrinter2, 2)
‘Loop

Do Until objTextFile.AtEndOfStream
strPrinter = objTextFile.Readline
strTest = Left(strPrinter, 2)

‘ If strTest = “\” Then
‘MsgBox strPrinter & ” ” & strPrinter2
WshNetwork.AddWindowsPrinterConnection strPrinter
if strPrinter = aqui Then

‘WshNetwork.SetDefaultPrinter strPrinter2
WshNetwork.SetDefaultPrinter aqui
End If
‘ End If

Loop

‘MsgBox “Impresoras Mapeadas”, 64, “Script Complete”

objTextFile.Close

‘ MsgBox “Printers have been mapped.” & vbCRLF & varDelText & vbCRLF & vbCRLF & “Impresoras Mapeadas”, 64, “Script Complete”
‘strComputer = “.”
‘Set objWMIService = GetObject(“winmgmts:” _
‘ & “{impersonationLevel=impersonate}!\” _
‘ & strComputer & “rootcimv2”)
‘Set colInstalledPrinters = objWMIService.ExecQuery _
‘ (“Select * from Win32_PrinterConfiguration “)
‘For Each objPrinter In colInstalledPrinters
‘ objPrinter.Duplex = False

‘MsgBox objPrinter.Duplex
‘Next
‘MsgBox “Finalizado. Impresora por defecto ” & aqui
Wscript.Quit

Aquí para descargarlo

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s