Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2310
  • Last Modified:

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.
0
klalakomacoi
Asked:
klalakomacoi
1 Solution
 
schmieguCommented:
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
0
 
klalakomacoiAuthor Commented:
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

0
 
klalakomacoiAuthor Commented:
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.
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
schmieguCommented:
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.
0
 
klalakomacoiAuthor Commented:
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?
0
 
schmieguCommented:
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)
0
 
klalakomacoiAuthor Commented:
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?
0
 
schmieguCommented:
Besides the infos from msdn.microsoft.com IMO the best site for scripting is:
http://cwashington.netreach.net

Other links I've bookmarked, but don't find so helpful:
http://home.att.net/~wshvbs/
http://desktopengineer.com/
0
 
klalakomacoiAuthor Commented:
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
0
 
KarelKCommented:
Array("User") in WBScript != new Array("User") in JavaScript. In JavaScript is Array an object.
0
 
EnterpriseAutomaterCommented:
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now