Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

Remapping network printers

I have 2 printers in an office here that i need to re map.
One is being de commissioned and the other moving into production.
We have changed the name slightley and have made a script to remove the old printers and add the new one.
What i need to add is something that will check and see if the printer is mapped or not already, and if its not mapped to the users computer then to skip removing the printer and adding it.

Here is the script i have now :
' Removing SHARP Sugarplumb

Dim WshPrinter, strPrinterPath
strPrinterPath1 = "\\caedmfsrv1\Sugarplumb"
strPrinterPath2 = "\\caedmfsrv1\FieryXP12"
Set WshPrinter = WScript.CreateObject("WScript.Network")
WshPrinter.RemovePrinterConnection strPrinterPath1, true, true  
WScript.Echo "Removed Printer: " & strPrinterPath1
WshPrinter.RemovePrinterConnection strPrinterPath2, true, true  
WScript.Echo "Removed Printer: " & strPrinterPath2

' Adding XEROX Sugarplumb
Dim objNetwork, strPrintPath
strPrintPath = "\\caedmfsrv1\Sugarplum"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrintPath
WScript.Echo "Connected Printer: " & strPrintPath
Wscript.Quit
' End of Script

So essentially the script should check to see if \\caedmfsrv1\Sugarplumb exists and if it does remove it then check to see if \\caedmfsrv1\FieryXP12 is there and remove it. Then if it removed 1 of those 2 then map the new printer.
0
mkps
Asked:
mkps
  • 3
  • 3
  • 2
1 Solution
 
nffvrxqgrcfqvvcCommented:
Option Explicit

'*******************************************************************
'   Name        :   modPrinters
'   Purpose     :   Provides functions for manipulating system
'                   printers
'*******************************************************************

'Declare API functions
Public Declare Function DeletePrinterConnection Lib "winspool.drv" Alias "DeletePrinterConnectionA" (ByVal pName As String) As Long

Public Function GetDefaultPrinter() As String

'*******************************************************************
'   Name        :   GetDefaultPrinter
'   Purpose     :   Retrieves the path of the current default printer
'                   on the local machine
'   Parameters  :   None
'   Returns     :   Default printer if successful, empty string if not
'*******************************************************************

'Declare local variables
Dim strReturn   As String
Dim prnDefault  As Printer

'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.GetDefaultPrinter"
   
On Error GoTo ErrorHandler
           
    'Set default return value
    strReturn = vbNullString
           
    'Get the current default printer
    Set prnDefault = Printer
   
    'Get the name of the default printer
    strReturn = prnDefault.DeviceName
       
CleanExit:

    'Kill the printer object
    If Not prnDefault Is Nothing Then
        Set prnDefault = Nothing
    End If

    GetDefaultPrinter = strReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function SetDefaultPrinter(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   SetDefaultPrinter
'   Purpose     :   Sets the default printer on the local machine
'   Parameters  :   v_strPrinterPath  : Path of printer to set as default
'   Returns     :   TRUE if successful, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.SetDefaultPrinter"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Set the printer to be the default
    objNetwork.SetDefaultPrinter v_strPrinterPath
   
    'Printer set
    blnReturn = True

CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    SetDefaultPrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function AddPrinter(ByVal v_strPrinterPath As String, _
                           Optional ByVal v_strPrinterDriver = "") As Boolean

'*******************************************************************
'   Name        :   AddPrinter
'   Purpose     :   Adds the specified printer to the local machine
'   Parameters  :   v_strPrinterPath  : Path of printer to add
'                   v_strPrinterDriver: Driver of printer to add (Optional)
'   Returns     :   TRUE if printer added, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.AddPrinter"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Add the printer
    objNetwork.AddWindowsPrinterConnection v_strPrinterPath, v_strPrinterDriver
       
    'Printer added and set
    blnReturn = True

CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    AddPrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function RemovePrinter(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   RemovePrinter
'   Purpose     :   Removes the specified printer from the local
'                   machine
'                   NB: API function used as RemovePrinterConnection
'                   method of Windows Script Host did not work!
'   Parameters  :   v_strPrinterPath   : Printer to remove
'   Returns     :   TRUE if printer removed, FALSE if not
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim lngReturn   As Long
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.RemovePrinter"
   
On Error GoTo ErrorHandler
       
    'Set the default return value
    blnReturn = False
       
    'Delete the printer
    lngReturn = DeletePrinterConnection(v_strPrinterPath)
   
    'Check the result
    If lngReturn <> 0 Then
   
        'Printer deleted
        blnReturn = True
       
    End If
   
CleanExit:

    RemovePrinter = blnReturn

    Exit Function

ErrorHandler:

    'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function GetAllConnectedPrinters() As String()

'*******************************************************************
'   Name        :   GetAllConnectedPrinters
'   Purpose     :   Retrives all printers currently connected to
'                   the local machine
'   Parameters  :   None
'   Returns     :   String array containing printer names
'*******************************************************************

'Declare local variables
Dim bytIndex    As Byte
Dim objNetwork  As Object
Dim objPrinters As Object
Dim bytCounter  As Byte
Dim strReturn() As String
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.GetAllConnectedPrinters"
   
On Error GoTo ErrorHandler

    'Set the index counter
    bytIndex = 0
   
    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Retrieve the printer details
    Set objPrinters = objNetwork.EnumPrinterConnections
   
    For bytCounter = 0 To objPrinters.Count - 1 Step 2
       
        'Re dimesion the array
        ReDim Preserve strReturn(bytIndex)
           
        'Populate the array
        strReturn(bytIndex) = objPrinters.Item(bytCounter + 1)
       
        'Increment the index
        bytIndex = bytIndex + 1
   
    Next bytCounter
   
CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    'Kill the printers object
    If Not objPrinters Is Nothing Then
        Set objPrinters = Nothing
    End If

    GetAllConnectedPrinters = strReturn()

    Exit Function

ErrorHandler:

   'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function

Public Function IsPrinterConnected(ByVal v_strPrinterPath As String) As Boolean

'*******************************************************************
'   Name        :   IsPrinterConnected
'   Purpose     :   Checks to see if the specified printer is
'                   connected to the local machine
'   Parameters  :   v_strPrinterPath   : Printer to find
'   Returns     :   TRUE if printer connected, FALSE if not
'
'*******************************************************************

'Declare local variables
Dim blnReturn   As Boolean
Dim objNetwork  As Object
Dim objPrinters As Object
Dim bytCounter  As Byte
   
'Declare local constants
Const FUNCTION_NAME As String = "modPrinters.IsPrinterConnected"
   
On Error GoTo ErrorHandler

    'Set the default return value
    blnReturn = False

    'Create the Script Host object
    Set objNetwork = CreateObject("WScript.Network")
   
    'Retrieve the printer details
    Set objPrinters = objNetwork.EnumPrinterConnections
   
    For bytCounter = 0 To objPrinters.Count - 1
       
        'Check for the specified printer
        If objPrinters.Item(bytCounter) = v_strPrinterPath Then
       
            'Printer Found
            blnReturn = True
            Exit For
           
        End If
       
    Next bytCounter
   
CleanExit:

    'Kill the script host object
    If Not objNetwork Is Nothing Then
        Set objNetwork = Nothing
    End If

    'Kill the printers object
    If Not objPrinters Is Nothing Then
        Set objPrinters = Nothing
    End If

    IsPrinterConnected = blnReturn

    Exit Function

ErrorHandler:

   'Display the error
    MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Has occured in " & FUNCTION_NAME & vbCrLf & _
           "Please contact Technical Support", vbOKOnly + vbCritical, App.Title
   
    Resume CleanExit
   
End Function
0
 
nffvrxqgrcfqvvcCommented:
'ADD THE ABOVE TO A .BAS MODULE
' And call each desired funtion
0
 
mkpsAuthor Commented:
Sorry,
I am kinda new to this whole thing, essentially I need this to run in a login script, im not really sure at this point how to compile all of that great code into somthing that cause the desired result.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nffvrxqgrcfqvvcCommented:
okay...you mean a .VBS file?
0
 
mkpsAuthor Commented:
yeah sorry
0
 
EDDYKTCommented:
You need to use EnumPrinterConnections Method


http://www.winguides.com/scripting/reference.php?id=132
0
 
mkpsAuthor Commented:
So that will tell me what printers are mapped.... How do i go about getting it to check if one is installed and then to remove it.
0
 
EDDYKTCommented:
you need to create function and compare the printers.item(i+1)



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now