Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI and the ADSI provider

Status
Not open for further replies.

scottcm

MIS
Jun 11, 2002
5
0
0
US
Normally I can translate VBScript examples into working JScript but I'm having trouble with this one. And please, no suggestions to write it in VBS. I need the code in JScript.

I'm trying to connect to the ADSI provider in WMI in order to do deep searches of the Active Directory for computer names. WMI provides easier method for doing this kind of query.

The following VBScript code will locate a computer located anywhere in Active Directory:

Set refWMI = GetObject("winMgmts:root\directory\LDAP")
Set colComputers = refWMI.ExecQuery("SELECT * FROM DS_Computer")
For Each refPC In colComputers
WScript.Echo refPC.DS_cn
Next

Just translating the syntax to JScript does not work. I don't get any errors. I also don't get any computer names.

Among the various syntax I've tried in JScript, this looked the most promising to me but it didn't work (no error but no computer's returned either).

var oLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var oTarget = oLocator.ConnectServer("", "root\\directory\\LDAP");
var ePC = new Enumerator(oTarget.ExecQuery("SELECT * FROM DS_User"));
for (var oPC in ePC)
WScript.Echo(oPC.DS_cn);

I've tried placing the name of a domain controller in place of the empty quotes on the ConnectServer line and it still didn't work.

Scott
 
Unless you are using Active X here (which I do not know) then javascript cannot do what you want.
 
I'm pretty sure it can be done in JScript without ActiveX. At least I've been able to bind to every WMI provider without it.

Most WMI code in JScript has to use the syntax:
var oLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var oTarget = oLocator.ConnectServer("", "root\\cimv2");

This code structure actually isn't unique to WMI. I have to use something similar to access the registry, COMAdmin, etc. My understanding (which could certainly be wrong) is that if JScript doesn't have native support for an external COM interface that you must use the new ActiveXObject reserved words. This doesn't mean you're really accessing ActiveX.

Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top