Advertisement
duck__boy1981

Add specific network printers

Dec 6th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. On Error Resume Next
  3.  
  4. '**
  5. ' Class to grab and set the relevant current user properties
  6. '*
  7. Class UserInfo
  8.    
  9.     Private m_currentUser   ' Object containing all of the details from the ADUC for the currently logged on user
  10.     Private m_userName      ' String for the User Name of the currently logged on user
  11.     Private m_displayName   ' String for the Display Name of the currently logged on user
  12.    
  13.     '**
  14.      ' Constructor
  15.      '*
  16.     Private Sub Class_Initialize()
  17.    
  18.         Call set_currentUser()
  19.         Call set_userName()
  20.         Call set_displayName()
  21.        
  22.     End Sub
  23.    
  24.     '**
  25.      ' Get/set the 'm_currentUser' property
  26.      '*
  27.     Public Property Get currentUser
  28.         Set currentUser = m_currentUser
  29.     End Property
  30.     Private Property Set currentUser(v)
  31.         Set m_currentUser = v
  32.     End Property
  33.    
  34.     '**
  35.      ' Get/set the 'm_userName' property
  36.      '*
  37.     Public Property Get userName
  38.         userName = m_userName
  39.     End Property
  40.     Private Property Let userName(v)
  41.         m_userName = v
  42.     End Property
  43.    
  44.     '**
  45.      ' Get/set the 'm_displayName' property
  46.      '*
  47.     Public Property Get displayName
  48.         displayName = m_displayName
  49.     End Property
  50.     Private Property Let displayName(v)
  51.         m_displayName = v
  52.     End Property
  53.    
  54.     '**
  55.      ' Set the 'currentUser' property
  56.      '*
  57.     Sub set_currentUser()      
  58.    
  59.         Dim objSysInfo     
  60.         Set objSysInfo = CreateObject("ADSystemInfo")
  61.        
  62.         Set currentUser = GetObject("LDAP://" & objSysInfo.UserName)
  63.        
  64.     End Sub
  65.    
  66.     '**
  67.      ' Set the 'userName' property
  68.      '*
  69.     Sub set_userName()
  70.         userName = currentUser.Get("userPrincipalName")
  71.     End Sub
  72.    
  73.     '**
  74.      ' Set the 'displayName' property
  75.      '*
  76.     Sub set_displayName()
  77.         displayName = currentUser.Get("displayName")
  78.     End Sub
  79.  
  80. End Class
  81.  
  82. '** Initilise an instance of the 'UserInfo' object *'
  83. Dim objUser : Set objUser = New UserInfo
  84.  
  85.  
  86. '****************************************************************************************'
  87. '* Setup the log file                                                                   *'
  88. '****************************************************************************************'
  89.  
  90. Dim objFSO          ' An instance of the File System Object, for accessing files
  91. Dim objLog          ' The log file for this script
  92. Dim strLog          ' The path to the log file for this script
  93. Dim strLogUser      ' The user name (and the date/time) to output in the log file
  94. Dim strLogResults   ' The results of the script to output in the log file
  95.  
  96. ' Set up the log object files
  97. Set objFSO = CreateObject("Scripting.FileSystemObject")
  98.  
  99. ' Set the log file location and first line of the output
  100. strLog = "\\videss\ScriptLogs$\add_network_printers.txt"
  101. strLogUser = "[" & Date & " @ " & Time & "] -- " & objUser.displayName & " (" & objUser.userName & ")"
  102.  
  103. '** Open the log file for editing *'
  104. If Not objFSO.FileExists(strLog) Then ' Did not exist, must create
  105.     Set objLog = objFSO.CreateTextFile(strLog, True)
  106. Else ' Already existed, just need to open and set to append
  107.     Set objLog = objFSO.OpenTextFile(strLog, 8, True)
  108. End If
  109.  
  110.  
  111. '****************************************************************************************'
  112. '* Set the network printers that should be added                                        *'
  113. '****************************************************************************************'
  114.  
  115. Dim strPrinterServer    ' The server on which the network printers reside
  116. Dim strPrinters         ' The network printers that should be installed
  117. Dim strPrinter          ' Temporary string for looping through 'strPrinters'
  118.  
  119. strPrinterServer = "\\videss.dynedrewett.com\"
  120. strPrinters = Array( _
  121.     "Sherborne Accounts", _
  122.     "Sherborne Barn", _
  123.     "Sherborne Family", _
  124.     "sm_scan1", _
  125.     "sm_scan2", _
  126.     "sm_scan3", _
  127.     "sm_scan4" _
  128. )
  129.  
  130.  
  131. '****************************************************************************************'
  132. '* Grab the current network printer connections                                         *'
  133. '****************************************************************************************'
  134.  
  135. Dim objNetwork, objPrinters
  136. Set objNetwork = CreateObject("WScript.Network")
  137. Set objPrinters = objNetwork.EnumPrinterConnections
  138.  
  139.  
  140. '****************************************************************************************'
  141. '* Work out which printers should be added and add them                                 *'
  142. '****************************************************************************************'
  143.  
  144. '** Loop through each of the network printers that should be mapped... *'
  145. For Each strPrinter In strPrinters
  146.    
  147.     Dim bolExists : bolExists = 0   ' Whether or not a network printer already exists (reset to 0 by default for each loop iteration)
  148.     Dim strPrinterFull : strPrinterFull = strPrinterServer & strPrinter
  149.    
  150.     '** Loop through all of the current network printers... *'
  151.     Dim x   ' Dummy for looping
  152.     For x = 0 To objPrinters.Count - 1 Step 2
  153.  
  154.         If Left(objPrinters.Item(x+1),2) = "\\" Then
  155.        
  156.             '** Set the name of the mapped printer that is currently being looped *'
  157.             Dim tmpPrinter : tmpPrinter = objPrinters.Item(x+1)
  158.            
  159.             '** Check to see if 'strPrinterFull' is already mapped *'
  160.             If strPrinterFull = tmpPrinter Then
  161.                 bolExists = 1
  162.                 Exit For
  163.             End If
  164.            
  165.         End If
  166.        
  167.     Next
  168.    
  169.     '** Add the network printer (if necessary) *'
  170.     If bolExists = 0 Then  
  171.         objNetwork.AddWindowsPrinterConnection strPrinterFull
  172.     End If
  173.    
  174.     '** Update the log file output (and check for errors) *'
  175.     If Err.Number = 0 Then ' No error, this printer has been added or already exists
  176.    
  177.         If bolExists = 0 Then ' Printer had been added
  178.             strLogResults = strLogResults & VBCrLf & "    Added  - " & strPrinter
  179.         Else ' Printer already exists
  180.             strLogResults = strLogResults & VBCrLf & "    Exists - " & strPrinter
  181.         End If
  182.    
  183.     Else ' Error adding this printer
  184.    
  185.         strLogResults = strLogResults & VBCrLf & "    Error  - " & strPrinter
  186.         strLogResults = strLogResults & VBCrLf & "        Error Number: " & Err.Number
  187.         strLogResults = strLogResults & VBCrLf & "        Description:  " & Err.Description
  188.        
  189.         '** Clear the error *'
  190.         Err.Clear
  191.        
  192.     End If
  193.    
  194. Next
  195.  
  196.  
  197. '****************************************************************************************'
  198. '* Finish up the and quit the script                                                    *'
  199. '****************************************************************************************'
  200.  
  201. '** Update and close the log file *'
  202. objLog.WriteLine(strLogUser & strLogResults & VBCrLf)
  203. objLog.Close
  204.  
  205. '** Reset the objects used by this script '*
  206. Set objUser = Nothing
  207. Set objNetwork = Nothing
  208. Set objPrinters = Nothing
  209. Set objFSO = Nothing
  210. Set objLog = Nothing
  211.  
  212. WScript.Quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement