Saturday, June 23, 2012



Script to add a network printer and set as default

Option Explicit
Dim strComputer, objNetwork, strUNCPrinter, objWMIService, WSHPrinters, DomainString, UserString, UserObj, WSHNetwork, colInstalledPrinters, objPrinter
strComputer = "."
strUNCPrinter = "\\SERVER\HP Officejet Series"
Set WSHNetwork = CreateObject("WScript.Network")
DomainString = WSHNetwork.UserDomain
'WScript.Echo  DomainString
UserString = WSHNetwork.UserName
'WScript.Echo  UserString
'Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
'strComputer = WSHNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set UserObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
Set WSHPrinters = objNetwork.EnumPrinterConnections
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
    Wscript.Echo  "Name: " & objPrinter.Name
Next
WScript.Echo "The default printer is 
now: " & strUNCPrinter
WScript.Quit

No comments: