|
Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.
|
Categories |
Connect to Printers in a Specific Location(Community)
Script Code
VBScript
'*********************************************************
' 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
Platforms
For online peer support, join
The Official Scripting Guys Forum!
To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are 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 sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts 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 sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.
|