Link to home
Start Free TrialLog in
Avatar of mhmservices
mhmservicesFlag for United States of America

asked on

printer Add/Delete Batch Script not working

I'm working on a vb batch script that adds and deletes printers. I'm not much of a scripter but have pieced together the code below. It seems that the deletion portion works as well as the skip portion but the install portion isn't working.




'On Error Resume Next
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const Version = 1
Const strComputer = "."
Const strResistryLoc  = "SOFTWARE\Helpdesk"
Const strTestRegistry = "map-printers"
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
 
Dim DoNotDeleteArray
DoNotDeleteArray = Array("\\hqfile1\HQFinance","\\hqfile1\HQ IT","\\hqfile1\HQMICR1","\\hqfile1\HQ Payroll HP P3005","\\hqfile1\HQPayroll","\\hqfile1\HQ_Paroll_2")
 
if NOT FindRegKey then
    'Delete all network printers
    DeleteNetworkPrinters()
   
    'USAGE:     MapPrinter "\\SERVER\PRINTER SHARE", "True"
    MapPrinter "\\hqfile1\hqguest", "False"
    MapPrinter "\\hqfile1\hq_ap", "False"
    MapPrinter "\\hqfile1\itcopier", "False"
    MapPrinter "\\hqfile1\hqrecruiting", "False"
    MapPrinter "\\hqfile1\hqmarketing", "False"
    MapPrinter "\\hqfile1\hrcopier", "False"
    MapPrinter "\\hqfile1\hqreceiving", "False"
    CreateRegKey()
End if
 
Sub DeleteNetworkPrinters()
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT DeviceID FROM Win32_Printer WHERE Local='False'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
    Set objNetwork = CreateObject("WScript.Network")
    For Each objItem In colItems
        strPrinter = objItem.DeviceID
        'wscript.echo strPrinter
        if NOT InArray(strPrinter,DoNotDeleteArray) then
            objNetwork.RemovePrinterConnection objItem.DeviceID, True, True
        end if
    Next
End Sub
 
Sub MapPrinter(sPrinterPath,sPrinterDefault)
    On Error Resume Next
    WSHNetwork.AddWindowsPrinterConnection sPrinterPath
    WScript.Sleep 300
    If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
        WSHNetwork.SetDefaultPrinter sPrinterPath
    End If
End Sub
 
Function FindRegKey  
    Set objReg   = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath   = strResistryLoc
    strValueName = strTestRegistry
    objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
    if IsNull(strValue) then
        strValue = 0
    end if
    if Version > CInt(strValue) then
        FindRegKey = False
    else
        FindRegKey = True
    end if
End Function
 
Sub CreateRegKey  
    Set objReg   = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    strKeyPath   = strResistryLoc
    strValueName = strTestRegistry
    strValue     = CSTR(Version)
    Return       = objReg.CreateKey(HKEY_CURRENT_USER, strKeyPath)
    objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
End Sub
 
Function InArray(item,A)
    Dim i
    For i=0 To UBound(A) Step 1
        If A(i) = item Then
            InArray=True
            Exit Function
        End If
    Next
    InArray=False
End Function
Avatar of Justin_W_Chandler
Justin_W_Chandler
Flag of United States of America image

Sorry, this is not batch script; it is VBScript.
You didn't create the WSHNetwork object before you used it.  To be more consistent, you could change it to objNetwork as used earlier in the script.

It would also be better to remove the quotes from "False" when calling the the MapPrinter subroutine and change its usage from a string to a binary value as below.


Sub MapPrinter(sPrinterPath, blnPrinterDefault)
    On Error Resume Next
    Set objNetwork = CreateObject("WScript.Network")
    objNetwork.AddWindowsPrinterConnection sPrinterPath
    WScript.Sleep 300
    If blnPrinterDefault = True Then
        objNetwork.SetDefaultPrinter sPrinterPath
    End If
End Sub

Open in new window

Avatar of mhmservices

ASKER

Shift-3,

Thank you for your help. Should I just replace my sub mapprinter with what you posted? Is it that simple?
That, and removing the quotes in the earlier section, would fix this specific problem.


    'USAGE:     MapPrinter "\\SERVER\PRINTER SHARE", True
    MapPrinter "\\hqfile1\hqguest", False
    MapPrinter "\\hqfile1\hq_ap", False
    MapPrinter "\\hqfile1\itcopier", False
    MapPrinter "\\hqfile1\hqrecruiting", False
    MapPrinter "\\hqfile1\hqmarketing", False
    MapPrinter "\\hqfile1\hrcopier", False
    MapPrinter "\\hqfile1\hqreceiving", False

Open in new window

Shift-3,


I will make those changes and test agian. I may not be able to test until tomorrow but I'll post back with my results.
Shift-3,


I've been testing the script and it seems to work well although it maps an unrecognized printer. It shows up as "HQ on hq" which is wrong. I'm going back through to see if I can figure out which entry is causing this.

I have a question also, is there a way to allow the user to select which printer they want as default?
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great information, my questions are thus:


1. Is that script ready to go our would I need to customize it to my situation any?
2. Would it be better to make that code its own script and call it from the other one or embed it at the end of my existing script?