I am setting up a Terminal Server 2008 which will be used by different client organisations, each with multiple individual user accounts. I would like each client organisation to have a drive mapped to \server\clients\

Their OU name is also their client name, so I would like to be able to find their current OU and then use it for the mapping command. The OUs are hierarchicals, so it is the bottom-most OU name I need.

Example
OU:
Dedicated Clients\AjaxCorp

Should get a drive mapped to
\\server1\shares\AjaxCorp

Any suggestions on how I can get the OU? I am sure it must be easy, I just haven't figured it out...

I did find information about how to do this with VB script, but as it is a whole new environment I thought it would be nice to use PowerShell instead.

link|improve this question

80% accept rate
feedback

3 Answers

This will get you the LDAP path to the current computer:

$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$strFilter = "(&(objectCategory=computer)(name=" + $env:computername + "))"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = $strFilter

$strPath = $objSearcher.FindOne().Path

From the result in $strPath, you should be able to build a network path to the share you need.

link|improve this answer
Hi, Thanks for this, it did point me in the right direction. However, I was after the user and the OU of the user, not the computer. As it happens, your solution just needed a few tweaks to be what I was after so thanks! – Frans Sep 10 '09 at 11:10
My fault, I understood you were after the OU of the computer... getting the user OU is quite similar, though. – Massimo Sep 10 '09 at 13:37
feedback

Find the DN of the User and parse it from that.

I'm not a Powershell expert, or even an amateur for that matter, but that's probably where I'd start.

Sorry I can't be of any more assistance than that, hopefully it's some food for thought.

link|improve this answer
feedback
up vote 0 down vote accepted

I eventually figured out how to get the OU from http://www.microsoft.com/technet/scriptcenter/resources/pstips/dec07/pstip1207.mspx and thought I would share the results. The code to get the OUs is this:

$strName = $env:username
$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter

$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()
$objUser.memberOf

However, I then had a realisation that using the OU would be fraught with difficulty as a user may be a member of multiple OU's. I therefore decided to use the Company field in the User object. From the same code above I can do

$strCompany = $objUser.Company

And then do my drive mapping.

This answers my original question, but just for interest I should mention that I then decided not to use PowerShell for the logon script after all: It is just too painful to deploy; You can't just put a .ps1 file in the Group Policy, you have to explicitly call Powershell.exe from a cmd file. So, I rewrote my script in vbscript in a few minutes and regretted that I hadn't started with that :)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.