Link to home
Start Free TrialLog in
Avatar of klalakomacoi
klalakomacoiFlag for Fiji

asked on

ADSI - filtering on the winnt:// provider with javascript

This silly little problem is giving me the absolute sh?ts at the moment.  I'm trying to enumerate groups and users on a particular domain, instead my script returns every damn object in the computer collection.  Examine the code below:

  var strDomain = "someDomain/someComputer";
  var Computer = GetObject("WinNT://" + strDomain);
  Computer.Filter = new Array("Group","User");
  // WScript.Echo(Computer.Filter);
  var romulus, remus; //enumeration placeholders
  romulus = new Enumerator(Computer);
  for(;!romulus.atEnd();romulus.moveNext())
    {
    remus = romulus.item();
    WScript.Echo(remus.Name);
    }
  // WScript.Echo("done");

basically this line
  Computer.Filter = new Array("Group","User");
is not doing what the advertisements say, instead of the output being limited to users and groups i'm getting services, devices etc.  I can comment out the line or assign all sorts of rubbish to the variable and makes absolutely no difference to the output.  What am i doing wrong?

Target and Source computer are the same - Winnt 4.0 SP6 with ads.exe (file version 4.71.1015.0) applied.

Regards,

etc.
Avatar of schmiegu
schmiegu

I'm not familiar with JScript, so I will try to translate it to VBScript.

I believe, the problem is in this line:
   Computer.Filter = new Array("Group","User");
I never saw that you could filter by 2 parameters at once

Computer.Filter = Array("user")
for each remus in Computer
  wscript.echo remus.name
next
Computer.filter = Array("group")
for each romulus in Computer
  wscript.echo romulus
next
Avatar of klalakomacoi

ASKER

well the MS documentation says you can filter on two parameters at once (i'll find the documentation in question and point you to it), anyway i'll try your suggestion and give it a rip and get back to you.

unfortunately i am not allowed to use VBscript for religious reasons.

regards,

Ian

nope doesn't make a difference if i drop it to only one parameter in the filter statement.

sorry about leaving this question for dead for so long, buried in other crap.

i'll get that MS doco i was talking about earlier and post it.
Since it doesn't work with only one filter parameter, there must be another problem.
You're using the 'new' keyword: I'm not familiar with JScript, but in VBScript it's not a valid keyword, while it is in VB. So maybe the line doesn't really filter the results the GetObject.
i've pulled out the "new" still no difference, all the objects are output and not a bit of filtering.  does this work the way its supposed to in VBscript?
Yes, it does. For purpose of verification I'm running this script at the moment:

Option Explicit
dim myDomains, myDom
dim myUsers, myComp
dim oFSO, oFile
set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFso.CreateTextFile("DomainUsers.csv", True)
  oFile.WriteLine("Name" & vbTab & "Full Name")
  set myUsers = GetObject("WinNT://MyDomain")
  myUsers.Filter = Array("User")
  for each myUser in myUsers
    oFile.WriteLine(myUser.Name & vbTab & myUser.FullName)
  next
oFile.Close
MsgBox "Done"

I only got user accounts. Changing the filter to "computer" only showed computers. (I only got an error, because I didn't delete the .FullName property from the script - this isn't supported by computers)
you're right, it works like a charm.  I'm sorry but have to leave you now, i have to do a ritual cleansing ceremony because i used VBscript.

Its obviously an issue between JavaScript and ADSI - JavaScript can't pass parameters to the Filter attribute properly.

you know any JavaScript nutcases who can shed light on this?
ASKER CERTIFIED SOLUTION
Avatar of schmiegu
schmiegu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well i guess thats the best we're going to get on this so i'll close the question and give schmeigu the points.  If i ever get any answer on how to do this in js i'll forward it on.

Schmiegu, post a comment at Q_20319811 and Q_20319810 to clean up the 40 available that side.

Regards and thanks for your help
Array("User") in WBScript != new Array("User") in JavaScript. In JavaScript is Array an object.
but you can convert a javascript array to a vbscript SAFEARRAY if you cheat and use the scripting.dictionary object..

I've tested this process with filtering for computers and users through GetObject("WinNT://<domain>") and it works exactly as expected -- though there are a few more hoops..


function JS2VBArray( objJSArray )
{
    var dictionary = new ActiveXObject( "Scripting.Dictionary" );
    for ( var i = 0; i < objJSArray.length; i++ )
    {
        dictionary.add( i, objJSArray[ i ] );
    }
 
    return dictionary.Items();
}
var myDomains, myDom;
var myUsers, myComp,myResults;
 
// Substitue <domain> with the netbios name of your domain;
myResults = GetObject("WinNT://<domain>");
 
myResults.Filter = JS2VBArray(new Array("Computer"));
 
//This line below creates a filter on two classes
//myResults.Filter = JS2VBArray(new Array("Computer,User"));
 
var e = new Enumerator(myResults);
for (; !e.atEnd();e.moveNext())
{
	myUser = e.item();
	WScript.StdOut.WriteLine(myUser.Name + ":\t" + myUser.Class);
	//oFile.WriteLine(myUser.Name + "," + myUser.FullName)
}

Open in new window