[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

printer Add/Delete Batch Script not working

Posted on 2009-04-16
9
Medium Priority
?
527 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:mhmservices
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24158714
Sorry, this is not batch script; it is VBScript.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24158946
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

0
 

Author Comment

by:mhmservices
ID: 24159047
Shift-3,

Thank you for your help. Should I just replace my sub mapprinter with what you posted? Is it that simple?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Expert Comment

by:Shift-3
ID: 24159159
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

0
 

Author Comment

by:mhmservices
ID: 24159184
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.
0
 

Author Comment

by:mhmservices
ID: 24183454
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?
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 24186653
See this article for an example of letting users choose a default printer.
0
 

Author Comment

by:mhmservices
ID: 24186776
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?


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question