I need to modifuy my script to find the users OU and connect to any printers that are in the same OU.
Here is the script so far.
Thanks
John Fuhrman
Here is the script so far.
Code:
'****************************************************
'* Script by: John Fuhrman
'* Date: 2008, August 21
'****************************************************
Option Explicit
'On Error Resume Next
Dim WSHShell: Set WSHShell = CreateObject("WScript.Shell")
Dim WSHNetwork: Set WSHNetwork = CreateObject("WScript.Network")
Const ADS_SCOPE_SUBTREE = 2
Dim objConnection: Set objConnection = CreateObject("ADODB.Connection")
Dim objCommand: Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
'Automatically find the domain name
Dim objDomain: Set objDomain = getObject("LDAP://rootDse")
Dim strDomain: strDomain = objDomain.Get("dnsHostName")
Dim strLDAPDomain: strLDAPDomain = "LDAP://" & objDomain.Get("defaultNamingContext")
Dim objLDAPDomain: Set objLDAPDomain = GetObject(strLDAPDomain)
' Get the currently logged in user
Dim strUserName: strUserName = WshNetwork.UserName
' Get the currently logged in users distinguished Name from AD.
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
Dim objUser: Set objUser = GetObject _
("LDAP://" & GetDistinguishedName(strUserName))
If strDomain = "" Then
'If strDomain <> "" Then
GetADprinters
Else
ConnectRemote
End If
Public Sub GetADprinters()
' Create dictionary object to store found printers
Dim objDictPrntrs: Set objDictPrntrs = CreateObject("Scripting.Dictionary")
objDictPrntrs.CompareMode = vbTextCompare
' Active Directory printer Enumoration
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select printerName, serverName from " _
& " '" & strLDAPDomain & "' where objectClass='printQueue'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Dim objRecordSet: Set objRecordSet = objCommand.Execute
' Loop through the list of printers in AD and add them to our dictionary object
' to be able to connect to them.
Do Until objRecordSet.EOF
objDictPrntrs.Add "\\" & objRecordSet.Fields("serverName").Value &_
"\" & objRecordSet.Fields("printerName").Value, _
UCase(objRecordSet.Fields("printerName").Value)
objRecordSet.MoveNext
Loop ' End Do Until Loop
' loop through printer connections
Dim WSHPrinters, LOOP_COUNTER
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
PrinterPath = WSHPrinters.Item(LOOP_COUNTER +1)
' if the current path exist in our dictionary remove it
' If objDictPrntrs.Exists(PrinterPath) Then WScript.Echo "printer exists"
If objDictPrntrs.Exists(PrinterPath) Then objDictPrntrs.Remove PrinterPath
Next
' Loop through found AD printers and connect to them.
Dim PrinterPath
For Each PrinterPath In objDictPrntrs.Keys
WSHNetwork.AddWindowsPrinterConnection PrinterPath
Next
End Sub
Sub ConnectRemote()
' Enumerate printers based on a list of print servers.
' Create disctionary object to store found printers
Dim objDictPrnts: Set objDictPrnts = CreateObject("Scripting.Dictionary")
objDictPrnts.CompareMode = vbTextCompare
' Create array with list of print servers
Dim arrPrintServers(1)
arrPrintServers(0) = "mdl-fp1"
arrPrintServers(1) = "mdl-vs1"
'arrPrintServers(2) = ""
'arrPrintServers(3) = ""
'arrPrintServers(4) = ""
'arrPrintServers(5) = ""
'arrPrintServers(6) = ""
'arrPrintServers(7) = ""
'arrPrintServers(8) = ""
'arrPrintServers(9) = ""
' Begin loop for each print server
Dim PrintServer
For Each PrintServer In arrPrintServers
'WScript.Echo PrintServer
' Enumerate all printers on Print server
Dim strPrnServer: strPrnServer = PrintServer
Dim objWMIService: Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strPrnServer & "\root\cimv2")
Dim colInstalledPrinters: Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
' Add printers found to a dictionary object for later
Dim objPrinter
For Each objPrinter In colInstalledPrinters
' create a dictionary to store our printer paths
On Error Resume Next
'WScript.Echo objPrinter.Name
objDictPrnts.Add "\\" & strPrnServer & "\" & objPrinter.Name, UCase(objPrinter.Name)
Next
' loop through printer connections
Dim WSHPrinters, LOOP_COUNTER
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
PrinterPath = WSHPrinters.Item(LOOP_COUNTER +1)
' if the current path exist in our dictionary remove it
' If objDictPrnts.Exists(PrinterPath) Then WScript.Echo "printer exists"
If objDictPrnts.Exists(PrinterPath) Then objDictPrnts.Remove PrinterPath
Next
' Loop through the printers add connect to them.
Dim PrinterPath
For Each PrinterPath In objDictPrnts.Keys
WSHNetwork.AddWindowsPrinterConnection PrinterPath
Next
Next
' End loop for each print server
End Sub
Function GetDistinguishedName(strUserName)
Const ADS_SCOPE_SUBTREE = 2
Dim objConnection, objCommand, strDomain, objRoot, objRecordSet
Set objRoot = GetObject("LDAP://rootDSE")
strDomain = objRoot.Get("defaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = ("ADsDSOObject")
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT distinguishedName FROM " & _
"'LDAP://" & strDomain & "' " & _
"WHERE samAccountName = '" & strUserName & "'"
objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
If Not objRecordSet.EOF Then
GetDistinguishedName = objRecordSet.Fields.Item("distinguishedName").Value
End If
End Function
Thanks
John Fuhrman