Announcement

Collapse
No announcement yet.

Trouble with recursion in WSH script (js) for reading AD info

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble with recursion in WSH script (js) for reading AD info

    I've got a script that I'm working on that recurse through OUs and grab name of each computer under the base DN given. The trouble that I'm having is that once the recursive function exits, the calling function exits without completing its loop. For example, given a structure like the following:
    Code:
    example.com
    |
    +-OU 1
    |  +-Computer A
    |  +-Computer B
    |
    +-OU 2
    |  +-Computer C
    |  +-Computer D
    |
    +-OU 3
       +-Computer E
       +-Computer F
    Only "OU 1" and the computers under it would be printed, then the loop exits without printing the rest.


    Here's the code...
    Code:
    function getComputerNamesFromAD(dn)
    {
    	var computerNames = new Array();
    
    	WScript.Echo("Looking up: \n" +  dn + "\n");
    	ldapObjects = GetObject("LDAP://" + dn);
    
    	for (
    			enumerator = new Enumerator(ldapObjects);
    			!enumerator.atEnd();
    			enumerator.moveNext()
    		)
    	{
    
    		var ldapObject = enumerator.item();	
    		var objectClass = ldapObject.Class;
    
    		if ( objectClass == "organizationalUnit" )
    		{
    			var objectRDN = ldapObject.Name;
    			var objectDN = objectRDN + ", " + baseDN
    
    			WScript.Echo("OU RDN: " + objectRDN + "\n");
    			//WScript.Echo("OU DN: " + objectDN)
    			
    			getComputerNamesFromAD(objectDN);
    		}
    	   	else if ( objectClass == "computer" )
    		{
    			WScript.Echo("Computer Name: " + ldapObject.CN);
    		}
    		else
    		{
    			WScript.Echo("Hit Something Else");
    		}
    
    
    
    		//computerNames.push(enumerator.item().CN);
    		//WScript.echo("object pushed" + " " + ldapObject.Class);
    	}
    
    	//return computerNames;
    }
    Thanks for your time.

  • #2
    Re: Trouble with recursion in WSH script (js) for reading AD info

    I have colored the little corrections (2) in blue,
    Code:
    function getComputerNamesFromAD(dn)
    {
    	var computerNames = new Array();
    
    	WScript.Echo("Looking up: \n" +  dn + "\n");
    	ldapObjects = GetObject("LDAP://" + dn)
    
    	for (
    			var en = new Enumerator(ldapObjects);
    			!en.atEnd();
    			en.moveNext()
    		)
    	{
    
    		var ldapObject = en.item();	
    		var objectClass = ldapObject.Class;
    
    		if ( objectClass == "organizationalUnit" )
    		{
    			var objectRDN = ldapObject.Name;
    			var objectDN = objectRDN + ", " + dn
    
    			WScript.Echo("OU RDN: " + objectRDN + "\n");
    			//WScript.Echo("OU DN: " + objectDN)
    			
    			getComputerNamesFromAD(objectDN);
    		}
    	   	else if ( objectClass == "computer" )
    		{
    			WScript.Echo("Computer Name: " + ldapObject.CN);
    		}
    		else
    		{
    			WScript.Echo("Hit Something Else");
    		}
    
    
    
    		//computerNames.push(enumerator.item().CN);
    		//WScript.echo("object pushed" + " " + ldapObject.Class);
    	}
    
    	//return computerNames;
    }
    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Trouble with recursion in WSH script (js) for reading AD info

      Man, I wasn't even looking at the statements in the for loop. Thanks for the help!

      Comment

      Working...
      X