Below is a printer script I am running through a GPO on an OU, If the
computer and printer are in a top level OU then it works ok. Once the
computer goes into an OU that is one OU level deeper it doesnt work. Have
configured each OU the same as far as printers computers an policies. I just
move the printer and computer to the next OU and it doesnt work. It's like
the script isn't reading deep enough.
DOMAIN.COM
OU 1 (works)
OU 2 (No working)
OU 3 (works)
OU 4 (No working)
Can anyone help?
__________________________________________________________________________________________________________________________
Option Explicit
Dim objWshNetwork '<Windows Script Host Network Object>
Dim strComputerName '<CN= + Computer name string>
Dim strDomain '<Domain Name string>
Dim strLDAPDomain '<String to query the LDAP server>
Dim arrayDomain '<Temp Array>
Dim intCount '<Temp Counter>
Dim objOUPrintQues '<Object to hold the list of Printers>
Dim objPrinter '<Printer Object>
Dim objOUComputers '<List of computer objects>
Dim objComputer '<Computer object>
Dim strComputerOU '<String of the OU the computer is in>
Dim objOUList '<List of OU objects>
Dim objOU '<OU Object>
Dim boolFound '<Flag that the computer is found>
'********************* Very important, change only this
*********************************************************
strDomain = "home.com" '<Enter the Domain name, KEEP THE QUOTES>
' Notes:
' Use the DNS name! ie domain.com
'
'*****************************************************************************************************************
Set objWshNetwork = WScript.CreateObject("WScript.Network") '<Make a new
network object>
strComputerName = "CN=" & objWshNetwork.ComputerName '<Get the current
computer name and add "CN=" to the begining
'strComputerName = "CN=JaysPC" '<Debugger>
strLDAPDomain = "LDAP://"&strDomain '<Set the LDAP string to get all
the AD OU Objects
On Error Resume Next '<Turn on Error Handling>
Set objOUList = GetObject(strLDAPDomain) '<Get all the AD OU Objects>
If err <> 0 Then '<Catch the error, exit if error>
WScript.Echo "Error: " & err.number & " The Domain: "&strLDAPDomain& " was
not found. Check the logon script or network connection. REF:AD Objects"
Set objWshNetwork = Nothing
Set objOUList = Nothing
WScript.Quit
End If
On Error Goto 0
'<---------------------------------------- FIND WHAT OU THE COMPUTER IS
N --------------------------------------------------------------------->
For Each objOU In objOUList '<Step through each OU in domain>
strLDAPDomain = "LDAP://"&strDomain&"/" &objOU.Name '<Add the OU to the
end of the LDAP string>
arrayDomain = split(strDomain,".") '<Break up the domain name into
parts>
For intCount = 0 to ubound(arrayDomain)
strLDAPDomain = strLDAPDomain & ", DC=" & arrayDomain(intCount) '<Add
the search base>
next
On Error Resume Next '<Turn on error handling>
Set objOUComputers = GetObject(strLDAPDomain) '<Get all the objects in
the current OU>
If err <> 0 Then '<Catch the error, exit if error>
WScript.Echo "Error: " & err.number & " The Domain: "&strLDAPDomain& " was
not found. Check the logon script or network connection. REF:OU Objects"
Set objWshNetwork = Nothing
Set objOUList = Nothing
Set objOU = Nothing
Set objOUComputers = Nothing
Set arrayDomain = Nothing
WScript.Quit
End If
On Error Goto 0
objOUComputers.Filter = Array("Computer") '<Filter the objects, only
care about computers>
For Each objComputer In objouComputers '<Step through each computer
in the OU>
If objComputer.Name = strComputerName then '<If the current computer
is in this OU...>
boolFound = TRUE '<Set found to TRUE>
Exit For '<Stop searching this OU>
end if
Next
If (boolFound) Then
Exit For '<Found the computer in this OU, stop searching the OUs>
End If
Next
'<------------------------------------------------------------------------------------------------------------------------------------------------------->
On Error Resume Next '<Turn on error handling>
Set objOUPrintQues = GetObject(strLDAPDomain) '<Get all the objects in
the current OU>
If err <> 0 Then '<Catch the error, exit if error>
WScript.Echo "Error: " & err.number & " The Domain: "&strLDAPDomain& " was
not found. Check the logon script or network connection. REF:Printer
Objects"
Set objWshNetwork = Nothing
Set objOUList = Nothing
Set objOU = Nothing
Set objOUComputers = Nothing
Set arrayDomain = Nothing
Set objComputer = Nothing
Set objOUPrintQues = Nothing
WScript.Quit
End If
On Error Goto 0
objOUPrintQues.Filter = Array("PrintQueue") '<Filter the objects, only
care about printers>
For Each objPrinter In objouPrintQues '<Step through each printer in
OU>
If Trim(LCase(objComputer.Description)) =
Trim(LCase(objPrinter.Description)) Then '<If the printer and computer
description are same>
'<----Descriptions are the same, install the printer AS
DEFAULT ----------------------------------------------->
'WScript.Echo "Adding the printer: " & objPrinter.strPrinterPath & " - as
Default" '<debugger>
SetUpPrinter objPrinter.PrinterPath, TRUE '<Install the Default
Printer>
'<------------------------------------------------------------------------------------------------------------->
Else
'<----Descriptions are different, install the printer
anyway -------------------------------------------------->
'WScript.Echo "Adding the printer: " & objPrinter.strPrinterPath & " - NOT
as Default" '<debugger>
SetUpPrinter objPrinter.PrinterPath, FALSE '<Install the Printer>
'<------------------------------------------------------------------------------------------------------------->
End If
Next
'<-- Clean up memory -->
Set objWshNetwork = Nothing
Set arrayDomain = Nothing
Set objOUPrintQues = Nothing
Set objPrinter = Nothing
Set objOUComputers = Nothing
Set objComputer = Nothing
Set objOUList = Nothing
Set objOU = Nothing
WScript.Quit
'<End of Main Program>
'<---------------------------------------------------------------------------------------------------------------------------------------------------->
Function SetUpPrinter(strPath,boolSetToDefault)
On Error Resume Next '<Turn on error handling>
objWshNetwork.AddWindowsPrinterConnection strPath '<Add the printer>
If err <> 0 Then '<Catch the error, return if error>
WScript.Echo "Error: " & err.number & " The Printer: "& strPath & " was
not found. Logoff and Login again or check the logon script or network
connection. If this persists, restart your server."
Exit Function
End If
On Error Goto 0 '<Turn off error handling>
If (boolSetToDefault) Then
objWshNetwork.SetDefaultPrinter strPath '<Set the printer to default>
End If
End Function
_________________________________________________________________________________________________________________________