Submitted By: Mark Fellensiek
Language: VBScript
Description: Determines the computer's location in Active Directory and then connects to all the printers found in that location.
Script Code
'*********************************************************
' Purpose: Looks up the computer's account location in the
' Active Directory, then finds all the printers
' at that location and connects them to the PC.
'
' Usage: Set the value below to the name of your domain
' eg: yourdomain.local
'
'*********************************************************
Const TheDomainName = "'LDAP://DC=yourdomain,DC=local'"
'*********************************************************
' no need to change anything below this line
'*********************************************************
'*********************************************************
' Lookup the recorded location for this computer
'*********************************************************
Const ADS_SCOPE_SUBTREE = 2
Set WshNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
ThisComputerLocation = objComputer.Location
'*********************************************************
' disconnect all network printers from the PC (optional)
'*********************************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = objNetwork.EnumPrinterConnections
For i = 0 to colPrinters.Count -1 Step 2
if lcase(left(colPrinters.Item (i+1),2)) = "\\" then
objNetwork.RemovePrinterConnection colPrinters.Item (i + 1), true, true
end if
Next
'*********************************************************
' Query the Directory for all printers at this location
'*********************************************************
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select uncName from " _
& TheDomainName & " where objectClass='printQueue' and Location='" & ThisComputerLocation &"'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
'objRecordSet.MoveFirst
'*********************************************************
' Connect them.
' and set one of them as the default (optional)
'*********************************************************
While Not objRecordSet.EOF
objNetwork.AddWindowsPrinterConnection objRecordSet.Fields("uncName").Value
'objNetwork.SetDefaultPrinter objRecordSet.Fields("uncName").Value
objRecordSet.MoveNext
Wend
Note: Not all scripts run on all versions of Windows.
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts or on the Script Center, please contact scripter@microsoft.com.
Disclaimer
This script is not supported under any Microsoft standard support program or service. The script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the script or documentation, even if Microsoft has been advised of the possibility of such damages.