none
Mapping default printer based on AD attribute RRS feed

  • Question

  • Hi all ! 

    Sorry for my english, i'll try to be clear... :)

    I need to run a VBscript on TSE 2008 R2 servers to set computer's default printer (not user). For this, I need to use the ClientName var. 

    The default printer name is written in the computer's description attribute in Active Directory. 

    I'm a beginner in VBscripting. I have parts of scripts working, but not everything.

    Here is my script (my comments are written with ''''''''''''''''''''''''''''''''''''''''''''): 

    'find clientname

    strComputer = "."
    Const ForAppending = 8
    Set oNetwork = wscript.CreateObject("wscript.network") 'Create network object
    Set objShell = Wscript.CreateObject("Wscript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    strUserName  = oNetwork.userName
    strComputerName  = oNetwork.computerName
    strClientName = objShell.ExpandEnvironmentStrings("%clientname%")

    ExecuteSearch = SearchDistinguishedName(strClientName)
    Public Function SearchDistinguishedName(ByVal vSAN)
    Const ADS_SCOPE_SUBTREE = 2
        Dim oRootDSE, oConnection, oCommand, oRecordSet
        Set oRootDSE = GetObject("LDAP://rootDSE")
        Set oConnection = CreateObject("ADODB.Connection")
        oConnection.Open "Provider=ADsDSOObject;"
        Set objCommand = CreateObject("ADODB.Command")
        objCommand.ActiveConnection = oConnection
    ldstring = "'LDAP://" & oRootDSE.get("defaultNamingContext") & "'" 
    objCommand.CommandText = "Select Name, distinguishedName from "& ldstring & " where objectClass='computer'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Timeout") = 30 
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    objCommand.Properties("Cache Results") = False 
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    Do Until objRecordSet.EOF
    If lcase(objRecordSet.Fields("Name").Value) = lcase(vSan) Then
        Wscript.Echo "Client Name: " & objRecordSet.Fields("Name").Value & vbCrLf _ 
        & "Location: " & objRecordSet.Fields("distinguishedName").Value
        'Wscript.Quit
    End If
        objRecordSet.MoveNext
    Loop
    End Function

    ' find canonical name
    On Error Resume Next
    ADSRoot = "DC=domain,DC=fr"
    Const ADS_SCOPE_SUBTREE = 2
    Set objConnection2 = CreateObject("ADODB.Connection")
    Set objCommand2 = CreateObject("ADODB.Command")
    objConnection2.Provider = "ADsDSOObject"
    objConnection2.Open "Active Directory Provider"
    Set objCommand2.ActiveConnection = objConnection2
    objCommand2.Properties("Page Size") = 1000
    objCommand2.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    objCommand2.CommandText = _
    "SELECT * FROM 'LDAP://" & ADSRoot & "' WHERE objectCategory='user' OR objectCategory='computer'"


    ''''''''''''''''''''''''''''''''''''Here, I use a input box to enter the clientname, cause I don't know how to re-use the clientname found in the lasts vars.

    srchTrm = LCase(InputBox("Please enter your search term.", "Active Directory Search"))
    WScript.Echo srchTrm
    i = 0

    If Len(srchTrm) < 4 or Instr(srchTrm, "=") Then
    WScript.Echo "Please enter a search term in excess of 3 characters. Don't use ""="""
    WScript.Quit
    End If

    Set objRecordSet2 = objCommand2.Execute
    objRecordSet2.MoveFirst

    Do Until objRecordSet2.EOF
    strADPath = LCase(objRecordSet2.Fields("adsPath").Value)

    If InStr(strADPath, srchTrm) Then
    Set objUser = GetObject(objRecordSet2.Fields("adsPath").Value)

    Select Case objUser.Class
    Case "user"
    strMsg = "Type: Person" & VbCrLf
    strMsg = strMsg & "Name: " & objUser.DisplayName & VbCrLf
    strMsg = strMsg & "Email: " & objUser.mail & VbCrLf
    strMsg = strMsg & "Telephone: " & objUser.telephoneNumber & VbCrLf
    strMsg = strMsg & "Department: " & objUser.department & VbCrLf
    strMsg = strMsg & "Title: " & objUser.title & VbCrLf
    Case "computer"
    strMsg = "Type: Computer" & VbCrLf
    strMsg = strMsg & "Name: " & objUser.Get("name") & VbCrLf
    strMsg = strMsg & "OS: " & objUser.operatingSystem & VbCrLf
    Case Else
    strMsg = "Unidentified" & VbCrLf
    End Select
    'strMsg = strMsg & "LDAP: " & objUser.adsPath & VbCrLf
    objUser.GetInfoEx Array("canonicalName"), 0
    strMsg = strMsg & "AD Path: " & objUser.canonicalName & VbCrLf
    objUser.GetInfoEx Array("description"), 0
    strMsg = strMsg & "Imprimante: " & objUser.description
    WScript.Echo strMsg & VbCrLf

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Here I want to use the "objuser.description" to set default printer
    Dim WshNetwork
    Set WshNetwork = CreateObject("WScript.Network")
    WshNetwork.SetDefaultPrinter "\\SRV-PRINT.domain.fr\" & objUser.description
    i = i + 1
    End If
    objRecordSet2.MoveNext
    Loop

    If Not i > 0 Then
    WScript.Echo "No results found."
    End If

    I think there's a most simple way to do this, but for the moment, I Don't know how to do. 

    Any help would be grateful. 

    Tuesday, December 30, 2014 3:06 PM

Answers

  • I would also note that there is no such thing as a default printer for a computer. A default printer is a user setting (it is stored in HKEY_CURRENT_USER for the current user), not a computer setting.

    With this in mind, you could of course use the SetDefaultPrinter method of the WshNetwork object and just put the script in the all users Startup group on the computer. In this way, any user that logs onto that computer will get that printer set as the default (of course, the printer must exist for for the current user to be able to set it as a default in the first place).


    -- Bill Stewart [Bill_Stewart]

    Tuesday, December 30, 2014 4:30 PM
    Moderator

All replies

  • To correctly connect printer per user we generally use Group Policy.  Login scripts for thishave notbeen necessary since WS 2003R2.  GP can centrally manage all user printers based on their identity and are managed centrally.

    It is not possible to understand what you are asking.  Why would the computers default printer be set in the description.  That is not very reliable.

    I recommend using Group Policy to set this.

    Note also that the computer description is not the same one that is in AD.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, December 30, 2014 3:29 PM
    Tuesday, December 30, 2014 3:27 PM
  • Hello. 

    We need to do this because each computer in the office as a network printer (or nearly each computer). I can't do a Group Policy for each computer in my domain. We cannot work with users accounts, only computers . 

    My problem is that if the user1 is connected on Comp1, he must have the printer near computer 1 as default printer.

    If user1 is connected on computer 2, at the other side of the office, he must have the printer near computer 2. So I cannot imagine a group policy to do this :). 

    We had the idea to use the descprition attribute in Active Directory to indicate the default printer to map.

    We must now make a script to get clientname on the TSE session, look for the computer account in Active Directory, read its description's attibute and map the default printer. 

    For example, my computer is COMP01. I use a TSE environment on SRVTSE01. 
    The script need to determine which computer (COMP01) is connected to the SRVTSE01 server (clientname var), search Active Directory computer account of COMP01, read the account's description attribute (for example PRINT01) and set \\SRV-PRINT\PRINT01 as the default printer on COMP01.

    Hope it's better with this example... 

    Tuesday, December 30, 2014 3:50 PM
  • Group Policy is the only way you canmap a computer default printer.  Once set any user conencting to that workstation will ssee that printer. 

    If you are taling about accessing the local users computrer printer via RDS then you can set TS to connect to the default user printer, THere is no need to "map" a printer. This is automatic. Just configure TS to do this.

    To learn how to configure RDS post your questions in the RDS forum.


    ¯\_(ツ)_/¯

    Tuesday, December 30, 2014 4:09 PM
  • I thought default printer setting was on user configuration in group policy console... And infortunately, I have no choice, my boss told me to make a script... :(

    I don't imagine how to set a specific default printer for each computer with this way. :)

    Tuesday, December 30, 2014 4:29 PM
  • I would also note that there is no such thing as a default printer for a computer. A default printer is a user setting (it is stored in HKEY_CURRENT_USER for the current user), not a computer setting.

    With this in mind, you could of course use the SetDefaultPrinter method of the WshNetwork object and just put the script in the all users Startup group on the computer. In this way, any user that logs onto that computer will get that printer set as the default (of course, the printer must exist for for the current user to be able to set it as a default in the first place).


    -- Bill Stewart [Bill_Stewart]

    Tuesday, December 30, 2014 4:30 PM
    Moderator
  • Bill, it's exactly what I wanna do. :p

    But I need to set a specific default printer for each computer in the office. We use TSE sessions, so I can't use %COMPUTERNAME% but %CLIENTNAME%. 

    My script needs to recognize the %CLIENTNAME%, go to the AD computer's account, read the description attribute who contains the name of the printer, and to set this printer as the default one on the computer... :)

    Tuesday, December 30, 2014 4:38 PM
  • I thought default printer setting was on user configuration in group policy console... And infortunately, I have no choice, my boss told me to make a script... :(

    I don't imagine how to set a specific default printer for each computer with this way. :)

    What you are asking is still not clear.  You say you want to set the printer that is defined in the computer description.  Why?  You say you want this set in the RDS session but the RDS session does not know about the connecting computer.  It can connect the users local printers to the remote session.  The only computer accessible in a session is the RDS computer but you say you want the users computer printer; RDS does this for you.

    Can you see why this is confusing.  RDS will connect the users printers and set the users printers as a default.  Just configure RDS to do this.

    If this is not what you are asking then you need to spend some time adjusting your description of what you need to do.


    ¯\_(ツ)_/¯

    Tuesday, December 30, 2014 4:41 PM
  • Bill, if there is only one pronter defined at the computer level and none at the user it would become the default.

    What is unclear is how RDS factors into this.


    ¯\_(ツ)_/¯

    Tuesday, December 30, 2014 4:43 PM
  • Bill, it's exactly what I wanna do. :p

    But I need to set a specific default printer for each computer in the office. We use TSE sessions, so I can't use %COMPUTERNAME% but %CLIENTNAME%. 

    My script needs to recognize the %CLIENTNAME%, go to the AD computer's account, read the description attribute who contains the name of the printer, and to set this printer as the default one on the computer... :)

    As posted - RDS will set this for you and it will always get it correct. RDS is designed to connect client printers. You can cahnge this for a whole TS or for a group of users. What is mapped in the users local computer will be the default in RDS.  Check to be sure Group Policy is not set to not allow this.  If GP is disbling this then a script is not likely to fix it.

    It also sounds likeyour "boss" is testing you.  He would know that one of the purpose for RDS is to make the correct printers always available.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, December 30, 2014 4:48 PM
    Tuesday, December 30, 2014 4:47 PM
  • One other thing you need to be aware of.  Ony administrators can retrieve the dedscription of a remote computer.  THe descritpion displayed on the screen is the AD description and not the local computers description.

    If you wanted to place thisinfo into AD then you canuse the script you have to retrive the AD description. THe script is currently returning the users descripion.


    ¯\_(ツ)_/¯

    Tuesday, December 30, 2014 5:03 PM