Page 1 of 1 1
Topic Options
#191812 - 2009-01-15 08:10 PM create directory structure for users and ou structures
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
I have not started on this yet. I am just checking to see if I will not be reinventing the wheel. Does anyone have a script that would read and convert
OU Structure
\OU1\OU2\User1

to

Dir Path structure
C:\OU1\OU2\User2

Top
#191816 - 2009-01-16 12:27 AM Re: create directory structure for users and ou structures [Re: coder]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Not directly, but please share more details on what it is you're trying to do. In most cases some Admin has already done what you're doing and there may be code or another method to use.
Top
#191826 - 2009-01-16 12:13 PM Re: create directory structure for users and ou structures [Re: NTDOC]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Do you want to do it for the current user or for an entire OU tree?

Be aware that some characters in the OU path may not be allowed in a directory path.

Top
#191834 - 2009-01-19 04:53 PM Re: create directory structure for users and ou structures [Re: Richard H.]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
This is basicly creating home ares for users, but we want it to mimic the OU structure to a directory structure.

This needs to be done for all sub-OUs of a single OU. I already have the rights and share commands to be executed.


Edited by coder (2009-01-19 04:56 PM)

Top
#191835 - 2009-01-19 05:22 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Well you could use the fnLDAPQuery() UDF, to obtain what the users current AD path is. http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=142043

Then you could split the result out, and determine whare you want to place the folder.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191838 - 2009-01-19 06:32 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
I am not very skilled in creating LDAP queries tho. I can place what I have so far.

 Code:
Dim $aAttributes,$sADsPath,$strFilter
Dim $aResults,$c,$r

$aAttributes = "DN"
$sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
$strFilter = "(&(objectCategory=person)(objectClass=user))" 
$aResults = fnLDAPQuery($aAttributes,$sADsPath,$strFilter)
 
For Each $r in $aResults
    If InStr($r,"=")
        Split($r,"=")[Ubound(Split($r,"="))]
    Else
        $r ?
    EndIf
Next

This is returning no results.

Top
#191840 - 2009-01-19 08:08 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
I don't think that you have the correct attibute. It is going to be part of the ADsPath. The attribute that you most likely want to use is the one that has the NT4 naming convention. (I do not recall what the attibute name is and I am not at work to look it up). Get youself a good LDAP browser (Softerra is what I use), and you can look at what all of the attibutes are.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191841 - 2009-01-19 08:54 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
This little browser from Chris S. should do the trick as well and you can review the code to see how it works as well.

Requires KiXforms from Shawn Tassie KiXforms

KiXforms - Active Directory Browser Version 2

Top
#191844 - 2009-01-19 09:34 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
I have slightly modified what I had. The problem I am having now is trying to set ADPath to a Dir Path structure.

Here is the modified code:
 Code:
Dim $aAttributes,$sADsPath,$strFilter
Dim $aUsers,$sUser
$RootPath = "D:\HomeFolderTest\"

 $aAttributes = "samAccountName", "AdsPath" 
 $sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")  
 $strFilter = "(&(objectClass=User)(Name=*))"  
  
 $aUsers = fnLDAPQuery($aAttributes,$sADsPath,$strFilter,"Name")  
 ? @ERROR " | " @SERROR " when running User Query"

; Ensuring Directories exist
 For $c = 0 to Ubound($aUsers)
    $DestDir = $RootPath + $aUsers[$c,1]
    ? "("+$c+")Checking Dir " + $DestDir
;   If dir does not exist create it.
 Next


The $DestDir is what I am looking at now. Currently it is still raw in the Distinguished type format.

Top
#191845 - 2009-01-19 09:46 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
You will need to parse out the $DestDir as you were doing in your first example. Then check for each directory as you move down the path.

Ex:

cn=myuser,ou=office,cn=hq,dn=fabrikom,dn=microsoft,dn=com

 Code:
$ar_DN = Split($DestDir,",")
For each $element in $ar_DN
  $dir = $dir + "\" + Split($Element,"=")[1]
  If Not Exist $Dir  
    mkdir($dir)
  EndIf
Next
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191853 - 2009-01-20 08:03 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
here is the current version of this file. I am getting an error saying that I am missing a ")" on line 24(The IofSubStr line). But I just cant see it. Can someone see anything I don't in here?

 Code:
call @Scriptdir + "\ADUtils.udf"
call @Scriptdir + "String.udf"
$output = RedirectOutput("C:\LDAPOutput.txt")
Dim $aAttributes,$sADsPath,$strFilter
Dim $aUsers,$sUser
$RootPath = "E:\HomeFolderTest"

 $aAttributes = "samAccountName", "AdsPath" 
 $sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")  
 $strFilter = "(&(objectClass=User)(objectClass=person)(Name=*))"  
  
 $aUsers = fnLDAPQuery($aAttributes,$sADsPath,$strFilter,"Name")  
 ;? @ERROR " | " @SERROR " when running User Query"

; Ensuring Directories exist
 For $c = 0 to Ubound($aUsers)
    $sDestDir = $RootPath + $aUsers[$c,1]
    $ar_DN = Split($sDestDir,",")
    For each $element in $ar_DN
        $dir = $dir + "\" + Split($Element,"=")[1]
        $sDestDir = $RootPath + $Dir
        If Not (Left($Dir,4)= "\PFS") 
         $sRmv = "\OU3\OU2\OU3"
         $Location = IofSubStr($sDestDir,$sRmv,1)
         $sDestDir = SubStr($sDestDir, 0,$Location) + SubStr($sDestDir, $Location + Len(sRmv), Len $sDestDir)
         ? "Create " + $sDestDir        
;          fnCreateHomeFolder($sDestDir)
        Else
          ? "Skipping Check for " + $Dir
        EndIf
;      EndIf
      Next
 Next
sleep 30
$output = RedirectOutput("")

Function fnCreateHomeFolder($Dir)
  if exist("$Dir")=0
    md "$Dir"
  	if @error=0
	  ? "Home Folder created at $Dir"
	else
	  ? "Home Folder was NOT created at $Dir"
	endif
  endif
EndFunction

Top
#191854 - 2009-01-20 08:19 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
What is IofSubStr ? Is it a variable? If so put a $ in front of it, if it is a UDF, you do not have it in your code.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191855 - 2009-01-20 08:31 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
IofSubStr is a function I found on the Kix boards created by Lonkero.

IofSubStr

Top
#191856 - 2009-01-20 08:36 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Is that in one of your called scripts? If not you will need to add this to either one of your called scripts or add it to your current script.

 Code:
function IofSubStr($Str,$Find,$Start)
 dim $
 $=split(substr($Str,$Start),$Find)
 $IofSubStr=iif(0=ubound($),0,len($[0])+$Start)
endfunction
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191857 - 2009-01-20 08:39 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
That is the one I called. I have it in String.udf (since it is related to string processing).

It cant figure where the error is coming from though.

Top
#191858 - 2009-01-20 09:12 PM Re: create directory structure for users and ou structures [Re: coder]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Try adding it directly to the script. It may help with the troubleshooting.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#191867 - 2009-01-21 02:33 PM Re: create directory structure for users and ou structures [Re: Gargoyle]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I'm not too sure exactly what you are trying to do, but this example constructs a path from the OU structure (requires fnLDAPQuery() UDF):
 Code:
Break ON
$=SetOption("Explicit","ON")

Dim $aAttributes,$sADsPath,$strFilter
Dim $aUsers,$iIndex,$sPath,$aOUs,$sElement,$sDSN

$aAttributes = "samAccountName", "AdsPath" 
$sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
$strFilter = "(&(objectClass=User)(objectClass=person)(Name=*))"

$aUsers = fnLDAPQuery($aAttributes,$sADsPath,$strFilter,"Name")

; Ensuring Directories exist
For $iIndex=0 To UBound($aUsers)
	$sDSN=$aUsers[$iIndex,1]
	; First drop the providor and CN information
	$sDSN=SubStr($sDSN,InStr($sDSN,",OU=")+4)
	; Now drop the domain information
	$sDSN=SubStr($sDSN,1,InStr($sDSN,",DC=")-1)
	; Finally, split the OU parts out
	$aOUs=Split($sDSN,",OU=")
	; Now create the path
	$sPath=""
	For Each $sElement in $aOUs
		$sPath="\"+$sElement+$sPath
	Next
	"Path for "+$aUsers[$iIndex,0]+"="+$sPath ?
Next

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 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.082 seconds in which 0.037 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