Page 1 of 1 1
Topic Options
#188039 - 2008-06-04 09:19 AM Active Directory description attribute
Martinj Offline
Fresh Scripter

Registered: 2008-06-04
Posts: 7
Hi.
I'm working on a login script and was interested in finding out if I can read attributes from active directory and act upon the result of this reading?

Just an example:
Read the office attribute from AD for the user that logs in and if the attribute says "officeA" I want to make a specific printer default.

Any suggestion?

Thanks in advance / Martin

Top
#188040 - 2008-06-04 09:28 AM Re: Active Directory description attribute [Re: Martinj]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yes it is possible to read user attributes from AD.
The example below will connect a printer and set is as default depending on the contents of the office field in the users properties.

 Code:
Break on

$objSysInfo = CreateObject("ADSystemInfo")
$strUser = $objSysInfo.UserName
$objUser = GetObject("LDAP://" + $strUser)
$office = $objUser.physicalDeliveryOfficeName

Select
	Case $office = "a"
		$rc = AddPrinterConnection("\\server\printer1")
		$rc = SetDefaultPrinter("\\server\printer1")
	Case $office = "b"
		$rc = AddPrinterConnection("\\server\printer2")
		$rc = SetDefaultPrinter("\\server\printer2")
	Case $office = "c"
		$rc = AddPrinterConnection("\\server\printer3")
		$rc = SetDefaultPrinter("\\server\printer3")
EndSelect


Edited by Mart (2008-06-04 09:30 AM)
Edit Reason: Removed weird formatting from the code.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#188041 - 2008-06-04 09:32 AM Re: Active Directory description attribute [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Oh and here's a list with the attributes that you can query.

http://www.kouti.com/tables/userattributes.htm
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#188042 - 2008-06-04 09:59 AM Re: Active Directory description attribute [Re: Mart]
Martinj Offline
Fresh Scripter

Registered: 2008-06-04
Posts: 7
Woho! That was a quick reply!
Will try this in the afternoon.
Thanks a lot for now, Mart!
:-)

Top
#188043 - 2008-06-04 10:07 AM Re: Active Directory description attribute [Re: Martinj]
Martinj Offline
Fresh Scripter

Registered: 2008-06-04
Posts: 7
Btw, will this reconnect printers everytime?
Is there a way to check if this printer connection has already been made and if so skip it?

/Martin

Top
#188044 - 2008-06-04 10:35 AM Re: Active Directory description attribute [Re: Martinj]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The example above will always try to connect a printer no matter if it exists or not.

There is a UDF that checks to see if a printer exists and you can do your magic on the outcome of the UDF.

A better way (in my book) is to just delete all network printers and connect the ones that the users requires. This way you will always have a clean printer setup for the user. If they change departments or no longer require a specific printer it will be removed and the correct printers will be set at the next logon. Also when you remove a printer from your network and do not replace it with the example below user will stop printing to it because it is no longer installed on the user’s computer after the next logon. This will save you some calls from users that have printed something to a printer that no longer exists.

A small example (requires the ArrayEnumKey() UDF):
 Code:
;======== Delete all network printers ========
;Delete all mapped network printers
$key = "HKEY_CURRENT_USER\Printers\Connections"
$printers = ArrayEnumKey($key)
For Each $printer in $printers
	$rc = DelKey($key + "\" + $printer)
Next


UDF Library » PrinterExist() - Check for existence of a Printer
UDF Library » ARRAYENUMKEY() - Creates an array of names of the subkeys contained in a registry key
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#188046 - 2008-06-04 02:07 PM Re: Case $office = "b" [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Mart's example is an excellent way to illustrate the concept of reading AD and acting upon the data. In practice, however, coding your script around people or places is not good practice.. (I'm speaking in general terms, and not specifically about the example here.)

My point is - if a printer changes, you need to modify your script. If you add or remove a location, you need to significantly modify your script. What if a location has two (or more) printers? Yes, you have to modify your script for that site.

Kix supports the use of INI files, which can be used like simple databases. You can create an INI file like this:
 Code:
[SITE1]
Printers=\\server\printer
Default=\\server\printer
[SITE2]Printers=\\server2\printer1;\\server2\printer2
Default=\\server2\printer2

Each site has its own section, listing the printer(s) to map, and the default printer. The sections "[SITEx]" are named exactly the same as the AD sites you're referencing. Printers are listed, separated with semi-colons when there's more than one.

The code that would replace the Select/EndSelect statements would look like this:
 Code:
; read the list of printers, and the default printer
$Printers = ReadProfileString('.\printers.ini', $Office, 'Printers')
$Default = ReadProfileString('.\printers.ini', $Office, 'Default')

; continue only if printers are defined for this site
If $Printers
  ; map all defined printers
  For Each $Printer in Split($Printers, ';')
    $rc = AddPrinterConnection($Printer)
  Next
  ; set one as a default
  $rc = SetDefaultPrinter($Default)
EndIf

Now, if things change, all you do is add or edit the entries in the INI file, not your script. The more times you put your hands in your script, the more chances you have for failure. Keep the script logic simple, even if it means adding some "complexity" of an external file to manage site or user-specific data. It will be well worth it in the long run.

NOTE: You may need to specify the full path to the INI file, or use @SCRIPTDIR\printers.ini instead.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#188047 - 2008-06-04 02:34 PM Re: Case $office = "b" [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Amen!
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 778 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.059 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org