Page 1 of 1 1
Topic Options
#205533 - 2012-08-04 03:23 AM Printers Based on OU
cjutting Offline
Fresh Scripter

Registered: 2010-02-05
Posts: 40
Loc: IA
Hey Guys,

Been a while, but have just changed jobs and getting back into coding kix again. I have a request from our director to add printers for computers based on the OU the computer is in, so that no matter who logs into those computers they will always get the same set of printers for that area.

SO I've got the adding printer part down... I've looked at InOU and InContainer and started to make some test scripts, but just not making progress.

Any suggestions?


Edited by cjutting (2012-08-04 03:23 AM)

Top
#205534 - 2012-08-04 05:21 AM Re: Printers Based on OU [Re: cjutting]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
What are you having a problem with?

This should give you the OU of the local computer (unable to test)...
 Code:
$computer = CreateObject("ADSystemInfo").ComputerName
$objComputer = GetObject("LDAP://"+$computer)
$OU = Split(Split($objComputer.Parent,",")[0],"=")[1]

You can then just check against that.

http://blogs.technet.com/b/heyscriptingg...belongs-to.aspx


Edited by ShaneEP (2012-08-04 05:25 AM)

Top
#205535 - 2012-08-04 06:26 AM Re: Printers Based on OU [Re: ShaneEP]
cjutting Offline
Fresh Scripter

Registered: 2010-02-05
Posts: 40
Loc: IA
Having it return the proper OU for the computer logging in...

Maybe not only that, but I'm open to any suggestions that you guys have and any examples you have as well. It's been a while. Need to get back into the Kix groove

Top
#205536 - 2012-08-04 05:26 PM Re: Printers Based on OU [Re: cjutting]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Well the code I posted above should return the first OU that the computer is in, and save it in the $OU variable. I compressed it into one line below and added an example of an IF check.

 Code:
$OU = Split(Split(GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent,",")[0],"=")[1]

If $OU = "OUName1"
   $nul = AddPrinterConnection("\\Server\Printer1")
   $nul = AddPrinterConnection("\\Server\Printer2")
Endif
If $OU = "OUName2"
   $nul = AddPrinterConnection("\\Server\Printer3")
   $nul = AddPrinterConnection("\\Server\Printer4")
Endif


Edited by ShaneEP (2012-08-04 05:32 PM)

Top
#205537 - 2012-08-04 05:42 PM Re: Printers Based on OU [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Or, if you would rather get the last portion of the OU tree you could use something like this...

 Code:
$OU = GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent
$OU = Split(Right($OU,-InStrRev($OU,"OU=")-2),",")[0]

If $OU = "OUName1"
   $nul = AddPrinterConnection("\\Server\Printer1")
   $nul = AddPrinterConnection("\\Server\Printer2")
Endif
If $OU = "OUName2"
   $nul = AddPrinterConnection("\\Server\Printer3")
   $nul = AddPrinterConnection("\\Server\Printer4")
Endif

Top
#205538 - 2012-08-04 05:45 PM Re: Printers Based on OU [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
This line...

$OU = GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent

is returning the OU in a format like so...

LDAP://OU=Terminal Servers,OU=Servers,OU=Computers,OU=Headquarters,DC=domain,DC=domain,DC=com

It's just a matter of manipulating that string to get the level that you want, and then comparing that to something in your script.

Top
#205539 - 2012-08-04 07:35 PM Re: Printers Based on OU [Re: ShaneEP]
cjutting Offline
Fresh Scripter

Registered: 2010-02-05
Posts: 40
Loc: IA
Thanks for that.. I'll look at it again on Monday.. Honestly I'm not opposed to doing an if and putting in the whole LDAP path either.. Good times.
Top
#205540 - 2012-08-04 07:38 PM Re: Printers Based on OU [Re: cjutting]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
That should work just as well. Something like...
 Code:
$OU = GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent

If Instr($OU,"Terminal Servers") AND InStr($OU,"Headquarters")
  ;Do Stuff...
Endif


Good luck.

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 793 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.056 seconds in which 0.015 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