Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- On Error Resume Next
- '**
- ' Class to grab and set the relevant current user properties
- '*
- Class UserInfo
- Private m_currentUser ' Object containing all of the details from the ADUC for the currently logged on user
- Private m_userName ' String for the User Name of the currently logged on user
- Private m_displayName ' String for the Display Name of the currently logged on user
- '**
- ' Constructor
- '*
- Private Sub Class_Initialize()
- Call set_currentUser()
- Call set_userName()
- Call set_displayName()
- End Sub
- '**
- ' Get/set the 'm_currentUser' property
- '*
- Public Property Get currentUser
- Set currentUser = m_currentUser
- End Property
- Private Property Set currentUser(v)
- Set m_currentUser = v
- End Property
- '**
- ' Get/set the 'm_userName' property
- '*
- Public Property Get userName
- userName = m_userName
- End Property
- Private Property Let userName(v)
- m_userName = v
- End Property
- '**
- ' Get/set the 'm_displayName' property
- '*
- Public Property Get displayName
- displayName = m_displayName
- End Property
- Private Property Let displayName(v)
- m_displayName = v
- End Property
- '**
- ' Set the 'currentUser' property
- '*
- Sub set_currentUser()
- Dim objSysInfo
- Set objSysInfo = CreateObject("ADSystemInfo")
- Set currentUser = GetObject("LDAP://" & objSysInfo.UserName)
- End Sub
- '**
- ' Set the 'userName' property
- '*
- Sub set_userName()
- userName = currentUser.Get("userPrincipalName")
- End Sub
- '**
- ' Set the 'displayName' property
- '*
- Sub set_displayName()
- displayName = currentUser.Get("displayName")
- End Sub
- End Class
- '** Initilise an instance of the 'UserInfo' object *'
- Dim objUser : Set objUser = New UserInfo
- '****************************************************************************************'
- '* Setup the log file *'
- '****************************************************************************************'
- Dim objFSO ' An instance of the File System Object, for accessing files
- Dim objLog ' The log file for this script
- Dim strLog ' The path to the log file for this script
- Dim strLogUser ' The user name (and the date/time) to output in the log file
- Dim strLogResults ' The results of the script to output in the log file
- ' Set up the log object files
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- ' Set the log file location and first line of the output
- strLog = "\\videss\ScriptLogs$\add_network_printers.txt"
- strLogUser = "[" & Date & " @ " & Time & "] -- " & objUser.displayName & " (" & objUser.userName & ")"
- '** Open the log file for editing *'
- If Not objFSO.FileExists(strLog) Then ' Did not exist, must create
- Set objLog = objFSO.CreateTextFile(strLog, True)
- Else ' Already existed, just need to open and set to append
- Set objLog = objFSO.OpenTextFile(strLog, 8, True)
- End If
- '****************************************************************************************'
- '* Set the network printers that should be added *'
- '****************************************************************************************'
- Dim strPrinterServer ' The server on which the network printers reside
- Dim strPrinters ' The network printers that should be installed
- Dim strPrinter ' Temporary string for looping through 'strPrinters'
- strPrinterServer = "\\videss.dynedrewett.com\"
- strPrinters = Array( _
- "Sherborne Accounts", _
- "Sherborne Barn", _
- "Sherborne Family", _
- "sm_scan1", _
- "sm_scan2", _
- "sm_scan3", _
- "sm_scan4" _
- )
- '****************************************************************************************'
- '* Grab the current network printer connections *'
- '****************************************************************************************'
- Dim objNetwork, objPrinters
- Set objNetwork = CreateObject("WScript.Network")
- Set objPrinters = objNetwork.EnumPrinterConnections
- '****************************************************************************************'
- '* Work out which printers should be added and add them *'
- '****************************************************************************************'
- '** Loop through each of the network printers that should be mapped... *'
- For Each strPrinter In strPrinters
- Dim bolExists : bolExists = 0 ' Whether or not a network printer already exists (reset to 0 by default for each loop iteration)
- Dim strPrinterFull : strPrinterFull = strPrinterServer & strPrinter
- '** Loop through all of the current network printers... *'
- Dim x ' Dummy for looping
- For x = 0 To objPrinters.Count - 1 Step 2
- If Left(objPrinters.Item(x+1),2) = "\\" Then
- '** Set the name of the mapped printer that is currently being looped *'
- Dim tmpPrinter : tmpPrinter = objPrinters.Item(x+1)
- '** Check to see if 'strPrinterFull' is already mapped *'
- If strPrinterFull = tmpPrinter Then
- bolExists = 1
- Exit For
- End If
- End If
- Next
- '** Add the network printer (if necessary) *'
- If bolExists = 0 Then
- objNetwork.AddWindowsPrinterConnection strPrinterFull
- End If
- '** Update the log file output (and check for errors) *'
- If Err.Number = 0 Then ' No error, this printer has been added or already exists
- If bolExists = 0 Then ' Printer had been added
- strLogResults = strLogResults & VBCrLf & " Added - " & strPrinter
- Else ' Printer already exists
- strLogResults = strLogResults & VBCrLf & " Exists - " & strPrinter
- End If
- Else ' Error adding this printer
- strLogResults = strLogResults & VBCrLf & " Error - " & strPrinter
- strLogResults = strLogResults & VBCrLf & " Error Number: " & Err.Number
- strLogResults = strLogResults & VBCrLf & " Description: " & Err.Description
- '** Clear the error *'
- Err.Clear
- End If
- Next
- '****************************************************************************************'
- '* Finish up the and quit the script *'
- '****************************************************************************************'
- '** Update and close the log file *'
- objLog.WriteLine(strLogUser & strLogResults & VBCrLf)
- objLog.Close
- '** Reset the objects used by this script '*
- Set objUser = Nothing
- Set objNetwork = Nothing
- Set objPrinters = Nothing
- Set objFSO = Nothing
- Set objLog = Nothing
- WScript.Quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement