none
Printer Script, under a tight timeline, please help! RRS feed

  • Question

  • Hey All, 

    I'm in a bit of a bind with a project for work and could use some assistance.  I have almost all of the hta code complete.  Long story short our users will use this hta to install printers on a local Win7 machine before connecting to a Citrix session which will redirect that printer.
    I am using the below code to pull back the serverName, printerName, and location just fine, where I am struggling is pulling back the Comment or Description field depending on how your looking at it.
    If I add comment to the "objCommand.CommandText" it comes back blank.
    If I add comment to the "objCommand.CommandText" it comes back blank.
    If I add description to the "objCommand.CommandText" it throws an error, the data is invalid
    If I add Description to the "objCommand.CommandText" it throws an error, the data is invalid

    I know there is data in this property, DSQuery will return the desired results but is slower and messier and I'd prefer not to use it if at all possible.

    VBScript not VB.net
    
    ' List All Published Printers
    
    c = 0
    
    Const ADS_SCOPE_SUBTREE = 2
    
    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 printerName, serverName, location, Comment from " _     
        & " 'LDAP://DC=firm,DC=saulewing,DC=net'  where objectClass='printQueue'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    
    
    
    Do Until objRecordSet.EOF
    	If c <= 5 Then
    		WScript.Echo "Printer Name: " & objRecordSet.Fields("printerName").Value
        	WScript.Echo "Server Name: " & objRecordSet.Fields("serverName").Value
        	WScript.Echo "Printer Location: " & objRecordSet.Fields("location").Value
        	WScript.Echo "Printer OTHER: " & objRecordSet.Fields("Comment").Value
        	objRecordSet.MoveNext
        	c = c + 1
        Else
        	WScript.Quit 8888
        End If
    Loop

    Tuesday, July 8, 2014 3:16 PM

Answers

  • You don't need the AD cmdlets. PowerShell can do it using the .NET objects also. For example:


    $searcher = [ADSISearcher] "(objectClass=printQueue)"
    $searcher.SearchRoot = [ADSI] ""
    $searcher.PropertiesToLoad.AddRange(@("name","servername","location","comment"))
    $searcher.PageSize = 500
    $searcher.SizeLimit = 0
    $searcher.SearchScope = "Subtree"
    $searchResults = $searcher.FindAll()
    foreach ( $searchResult in $searchResults ) {
      $name = $searchResult.Properties["name"][0]
      $serverName = $searchResult.Properties["servername"][0]
      if ( $searchResult.Properties["location"] ) {
        $location = $searchResult.Properties["location"][0]
      }
      else {
        $location = ""
      }
      if ( $searchResult.Properties["comment"] ) {
        $comment = $searchResult.Properties["comment"][0]
      }
      else {
        $comment = ""
      }
      new-object PSObject -property @{
        "name" = $name
        "serverName" = $serverName
        "location" = $location
        "comment" = $comment
      } | select-object name,ServerName,location,Comment
    }
    $searchResults.Dispose()
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Nikolai5005 Tuesday, July 8, 2014 4:19 PM
    Tuesday, July 8, 2014 4:09 PM
    Moderator

All replies

  • This is a lot easier if you use PowerShell and the AD cmdlets:


    PS C:\> get-adobject -ldapfilter "(objectClass=printQueue)" -properties serverName,location,comment |
      select-object Name,serverName,location,comment |
      export-csv printQueues.csv -notypeinformation
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 8, 2014 3:21 PM
    Moderator
  • Thanks for your reply.  Typically I would have to agree with you.  However, most of our server infrastructure hasn't been updated to 2008 or above yet, still running a lot of 2003 servers. That being said, get-adobject is unable to find a default server with Active Directory Web Services running.

    Can you think of anything that is VBScript based to pull in the desired printer comments?

    Tuesday, July 8, 2014 4:02 PM
  • You don't need the AD cmdlets. PowerShell can do it using the .NET objects also. For example:


    $searcher = [ADSISearcher] "(objectClass=printQueue)"
    $searcher.SearchRoot = [ADSI] ""
    $searcher.PropertiesToLoad.AddRange(@("name","servername","location","comment"))
    $searcher.PageSize = 500
    $searcher.SizeLimit = 0
    $searcher.SearchScope = "Subtree"
    $searchResults = $searcher.FindAll()
    foreach ( $searchResult in $searchResults ) {
      $name = $searchResult.Properties["name"][0]
      $serverName = $searchResult.Properties["servername"][0]
      if ( $searchResult.Properties["location"] ) {
        $location = $searchResult.Properties["location"][0]
      }
      else {
        $location = ""
      }
      if ( $searchResult.Properties["comment"] ) {
        $comment = $searchResult.Properties["comment"][0]
      }
      else {
        $comment = ""
      }
      new-object PSObject -property @{
        "name" = $name
        "serverName" = $serverName
        "location" = $location
        "comment" = $comment
      } | select-object name,ServerName,location,Comment
    }
    $searchResults.Dispose()
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Nikolai5005 Tuesday, July 8, 2014 4:19 PM
    Tuesday, July 8, 2014 4:09 PM
    Moderator
  • Description is the field you are looking for. It's also returned as an array, since it's a multi-line field.

    Try this:

    c = 0
    
    Const ADS_SCOPE_SUBTREE = 2
    
    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 printerName, serverName, location, description from " _     
        & " 'LDAP://DC=firm,DC=saulewing,DC=net'  where objectClass='printQueue'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    
    
    Do Until objRecordSet.EOF
    	If c <= 5 Then
    		WScript.Echo "Printer Name: " & objRecordSet.Fields("printerName").Value
    		WScript.Echo "Server Name: " & objRecordSet.Fields("serverName").Value
    		WScript.Echo "Printer Location: " & objRecordSet.Fields("location").Value
    		arrDescription = objRecordSet.Fields("description").Value
    
    		for each strDescription in arrDescription
    			WScript.Echo "Printer OTHER: " strDescription
    		next
    		objRecordSet.MoveNext
    		c = c + 1
    	Else
    		WScript.Quit 8888
    	End If
    Loop


    Tuesday, July 8, 2014 4:14 PM
  • Thank you so much!

    I had to change the comment to description but it worked like a charm.  Huge life saver.

    Tuesday, July 8, 2014 4:19 PM
  • printqueue does not have an attribute "comment".  "Comment" is located on the printer object only.

    Bill is giving you good advice.  Give up on VBScript.  PowerShell is much easier.

    Here is the VBScript solution.  Further changes will be just as difficult

    Set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
    Set rs  = conn.Execute("Select name,ServerName,location,description from 'LDAP://DC=KAHLNET,DC=local' where objectclass='printqueue'")
    
    With rs
         Do Until .EOF
              if Not IsNull(.Fields("location")) Then 
                  location = .Fields("location")
              Else
                  location="no location"
              End If
              if Not IsNull(.Fields("description")) Then 
                  description = Join(.Fields("description"),"|")
              Else
                  description="no description"
              End If
              WScript.Echo .Fields("Name"),.Fields("serverName"),location ,description
             .MoveNext
         Loop
    End With


    ¯\_(ツ)_/¯

    Tuesday, July 8, 2014 4:20 PM