<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vbscriptblog.com &#187; printers</title>
	<atom:link href="http://vbscriptblog.com/category/vbscript/active-directory/printers/feed/" rel="self" type="application/rss+xml" />
	<link>http://vbscriptblog.com</link>
	<description>Scripting for Windows Sysadmins</description>
	<lastBuildDate>Mon, 16 Jan 2012 09:41:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file</title>
		<link>http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/</link>
		<comments>http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/#comments</comments>
		<pubDate>Fri, 27 May 2011 14:11:57 +0000</pubDate>
		<dc:creator>dirk adamsky</dc:creator>
				<category><![CDATA[printers]]></category>
		<category><![CDATA[active directory]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[command prompt]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[cscript]]></category>
		<category><![CDATA[enumerate]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[user]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vbs]]></category>
		<category><![CDATA[vbscript]]></category>
		<category><![CDATA[wmi]]></category>

		<guid isPermaLink="false">http://deludi.nl/blog/?p=1097</guid>
		<description><![CDATA[Today&#8217;s script is made for Hassan. He asked me to do a script that enumerates the printers of each user. Although that sounds easy it is a little complicated. The printers of each user are saved in the user profile (in the ntuser.dat file to be precisely). The easiest way to get a list of [...]]]></description>
			<content:encoded><![CDATA[<p>Today&#8217;s script is made for <a href="http://deludi.nl/blog/vbscript/active-directory/sendasrights/active-directory-vbscript-to-enumerate-the-send-as-rights-on-a-user-or-resource-account/">Hassan</a>.<br />
He asked me to do a script that enumerates the printers of each user. Although that sounds easy it is a little complicated.</p>
<p>The printers of each user are saved in the user profile<br />
(in the ntuser.dat file to be precisely).<br />
The easiest way to get a list of printers per user is to add a small script to the <a href="http://technet.microsoft.com/en-us/library/cc758918%28WS.10%29.aspx">logon script</a> (can also be done with a <a href="http://en.wikipedia.org/wiki/Group_Policy">group policy</a>).<br />
The script below is such a script.<br />
It is very compact and can be run under the users credentials.<br />
The script does a WMI query (the <a href="http://msdn.microsoft.com/en-us/library/aa394363%28v=vs.85%29.aspx">Win32_Printer Class</a>) for the printers and writes the output to a file.<br />
Unfortunately we cannot use the same file for all users because of potential &#8220;file locking&#8221; problems.<br />
This can be adressed by using a database but that is beyond the scope of this article.</p>
<p>Follow the next steps to run the script (no admin rights needed):</p>
<p>* open your favorite text editor<br />
* copy and paste the script into the editor<br />
* change the UNC path (&#8220;\srvXXXlogfiles&#8221;) in the LogToFile function on line 19 to your UNC path<br />
* save the script (for example c:tempprinters.vbs)<br />
* open a command prompt<br />
* go to &#8220;c:\temp&#8221;<br />
* give &#8220;cscript printers.vbs&#8221; (without quotes) and enter</p>
<p>To run the script as a logon script you can copy the script to the netlogon share or another user accessible location.</p>
<p>The script:</p>
<pre class="brush: vb; title: ; notranslate">
' Name : printers.vbs
' Description : script to enumerate the printers of Active Directory users and write the results to a file
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 27-05-2011

Set objNetwork = CreateObject(&quot;Wscript.Network&quot;)
Set objWMIService = GetObject(&quot;winmgmts:\.rootcimv2&quot;)
Set colPrinters = objWMIService.ExecQuery(&quot;Select * From Win32_Printer&quot;)
For Each objPrinter in colPrinters
    LogToFile(objNetwork.UserDomain &amp; &quot;&quot; &amp; objNetwork.UserName &amp; &quot; ; &quot; &amp; objPrinter.Name)
Next
Set colPrinters = Nothing
Set objWMIService = Nothing
Set objNetwork = Nothing 

Function LogToFile(Message)
Set ObjFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objTextFile = ObjFSO.OpenTextFile(&quot;\srvXXXlogfiles&quot; &amp; objNetwork.UserName &amp; Date &amp; &quot;.txt&quot;,8,True)
objTextFile.WriteLine Message
objTextFile.Close
Set objTextFile = Nothing
Set ObjFSO = Nothing
End Function
</pre>
<p>When you have problems/questions with the script please post a reply.</p>
<p>Happy scripting.</p>
<p>Best regards,</p>
<p>Dirk Adamsky</p>
<div class="bottomcontainerBox" style="background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fvbscriptblog.com%2Fvbscript%2Factive-directory%2Fprinters%2Fvbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/"></g:plusone>
			</div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/"  data-text="VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file" data-count="horizontal" data-via="dirkadamsky">Tweet</a>
			</div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div>]]></content:encoded>
			<wfw:commentRss>http://vbscriptblog.com/vbscript/active-directory/printers/vbscript-vbscript-to-enumerate-the-printers-of-active-directory-users-and-write-the-results-to-a-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Active Directory: VBscript to enumerate all Active Directory published printers</title>
		<link>http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/</link>
		<comments>http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 12:50:36 +0000</pubDate>
		<dc:creator>dirk adamsky</dc:creator>
				<category><![CDATA[printers]]></category>
		<category><![CDATA[active directory]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[command prompt]]></category>
		<category><![CDATA[cscript]]></category>
		<category><![CDATA[enumerate]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[printer]]></category>
		<category><![CDATA[printqueue]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vbs]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://deludi.nl/blog/?p=435</guid>
		<description><![CDATA[Topic of today is printing. I have made a script that enumerates all Active Directory published printers. By adding or removing attributes from arrAttributes you can change the output to your specific needs. Follow the next steps to run the script (no admin rights needed): * open your favorite text editor * copy and paste [...]]]></description>
			<content:encoded><![CDATA[<p>Topic of today is printing.<br />
I have made a script that enumerates all Active Directory published printers.<br />
By adding or removing attributes from arrAttributes you can change the output to your specific needs. </p>
<p>Follow the next steps to run the script (no admin rights needed):</p>
<p>* open your favorite text editor<br />
* copy and paste the script into the editor<br />
* save the script (for example c:\temp\adprinters.vbs)<br />
* open a command prompt<br />
* go to &#8220;c:\temp&#8221;<br />
* give &#8220;cscript adprinters.vbs&#8221; (without quotes) and enter</p>
<p>The script:</p>
<pre class="brush: vb; title: ; notranslate">
' Name : adprinters.vbs
' Description : script to enumerate all Active Directory published printers
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 16-03-2010
' Level : intermediate

arrAttributes = Array(&quot;name&quot;,&quot;printerName&quot;,&quot;location&quot;,&quot;serverName&quot;) 

Set adoCommand = CreateObject(&quot;ADODB.Command&quot;)
Set adoConnection = CreateObject(&quot;ADODB.Connection&quot;)
adoConnection.Provider = &quot;ADsDSOObject&quot;
adoConnection.Open &quot;Active Directory Provider&quot;
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
strBase = &quot;&lt;LDAP://&quot; &amp; objRootDSE.Get(&quot;defaultNamingContext&quot;) &amp; &quot;&gt;&quot;
Set objRootDSE = Nothing

strFilter = &quot;(objectClass=printQueue)&quot;
strAttributes = Join(arrAttributes,&quot;,&quot;)
Wscript.Echo Join(arrAttributes,&quot;;&quot;)
strQuery = strBase &amp; &quot;;&quot; &amp; strFilter &amp; &quot;;&quot; &amp; strAttributes &amp; &quot;;subtree&quot;
adoCommand.CommandText = strQuery
adoCommand.Properties(&quot;Page Size&quot;) = 100
adoCommand.Properties(&quot;Timeout&quot;) = 30
adoCommand.Properties(&quot;Cache Results&quot;) = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	On Error Resume Next
	strTempOutput = &quot;&quot;
	For i = 0 To Ubound(arrAttributes)
		strTempOutput =  strTempOutput &amp; &quot; ; &quot; &amp; adoRecordset.Fields(arrAttributes(i)).Value
		strOutput = Mid(Ltrim(strTempOutput),3)
	Next
	Wscript.Echo strOutput
	adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
</pre>
<p>Jérémie (see comment below) had a problem with adding the &#8220;description&#8221; attribute, the reason for the error is that the description attribute is of type &#8220;array&#8221; or &#8220;multi-value&#8221;.<br />
I just made an updated version with the description attribute.</p>
<p>The updated script:</p>
<pre class="brush: vb; title: ; notranslate">
' Name : adprinters.vbs
' Description : script to enumerate all Active Directory published printers with description attribute
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 13-07-2011
' Level : intermediate

arrAttributes = Array(&quot;name&quot;,&quot;printerName&quot;,&quot;location&quot;,&quot;serverName&quot;) 

Set adoCommand = CreateObject(&quot;ADODB.Command&quot;)
Set adoConnection = CreateObject(&quot;ADODB.Connection&quot;)
adoConnection.Provider = &quot;ADsDSOObject&quot;
adoConnection.Open &quot;Active Directory Provider&quot;
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
strBase = &quot;&lt;LDAP://&quot; &amp; objRootDSE.Get(&quot;defaultNamingContext&quot;) &amp; &quot;&gt;&quot;
Set objRootDSE = Nothing

strFilter = &quot;(objectClass=printQueue)&quot;
strAttributes = Join(arrAttributes,&quot;,&quot;) &amp; &quot;,description&quot;
Wscript.Echo strAttributes
strQuery = strBase &amp; &quot;;&quot; &amp; strFilter &amp; &quot;;&quot; &amp; strAttributes &amp; &quot;;subtree&quot;
adoCommand.CommandText = strQuery
adoCommand.Properties(&quot;Page Size&quot;) = 100
adoCommand.Properties(&quot;Timeout&quot;) = 30
adoCommand.Properties(&quot;Cache Results&quot;) = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	On Error Resume Next
	strTempOutput = &quot;&quot;
	For i = 0 To Ubound(arrAttributes)
		strTempOutput =  strTempOutput &amp; &quot; ; &quot; &amp; adoRecordset.Fields(arrAttributes(i)).Value
		strOutput = Mid(Ltrim(strTempOutput),3)
	Next
	arrDescription = adoRecordset.Fields(&quot;description&quot;).Value
	For Each strDescription in arrDescription
		Wscript.Echo strOutput &amp; &quot; ; &quot; &amp; strDescription
	Next
	adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
</pre>
<p>When you have problems/questions please post a reply, you can also rate the script.</p>
<p>Happy scripting.</p>
<p>Best regards,</p>
<p>Dirk Adamsky &#8211; Deludi BV</p>
<div class="bottomcontainerBox" style="background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fvbscriptblog.com%2Fvbscript%2Factive-directory%2Fprinters%2Factive-directory-vbscript-to-enumerate-all-active-directory-published-printers%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/"></g:plusone>
			</div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/"  data-text="Active Directory: VBscript to enumerate all Active Directory published printers" data-count="horizontal" data-via="dirkadamsky">Tweet</a>
			</div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div>]]></content:encoded>
			<wfw:commentRss>http://vbscriptblog.com/vbscript/active-directory/printers/active-directory-vbscript-to-enumerate-all-active-directory-published-printers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

