将活动目录查询从VBS转换为Javascript以获取全局编录

时间:2014-05-19 10:41:01

标签: javascript vbscript active-directory

任何人都可以在这里填空。 我一直在尝试获取一个脚本,我可以运行以查询全局目录中的所有可用用户的活动目录,最后在VBS中管理它以查找任何特定的用户名,如下所示:

Const ADS_SECURE_AUTHENTICATION = 1 


Set oGC = GetObject("GC:") 
For Each child In oGC 
Set oEntrprise = child 
Exit For 
Next 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.CreateTextFile("AD.txt", True) 

' Setup ADO. 
Set oConn = CreateObject("ADODB.Connection") 
Set oComm = CreateObject("ADODB.Command") 

oConn.Provider = "ADsDSOObject" 
oConn.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION 

oConn.Open 
oComm.ActiveConnection = oConn 

' Set the search command and filter. 
objFile.WriteLine(oEntrprise.ADsPath) 
oComm.CommandText = "<" & oEntrprise.ADsPath & ">;(&(objectCategory=person)(objectClass=user)(givenName=aaron*));cn,distinguishedName;subTree"
' Execute the query. 
Set oRS = oComm.Execute 

' Print the results. 
oRS.MoveFirst 
While Not oRS.EOF 
For Each field In oRS.Fields 
objFile.WriteLine(field) 
Next 
objFile.WriteLine("") 
oRS.MoveNext 
Wend 

WScript.Echo "Finished"

我现在正在尝试将其转换为JS,但我无法复制它。 我无法找到循环GetObject(“GC:”)的黄金答案。对于每个似乎在这种情况下似乎工作。有人知道怎么做吗? 所以实际上我需要在上面的脚本中使用JS equivelant of oEntrprise。

var oConn = WScript.CreateObject("ADODB.Connection");
var oComm = WScript.CreateObject("ADODB.Command"); 
var keyname = "samaccountname";
var keyvalue = "aaron";

oConn.Provider = "ADsDSOObject";
oConn.Properties("ADSI Flag") = 1; 

oConn.Open;
oComm.ActiveConnection = oConn;

var objRootDSE = GetObject("GC:");  
for (var i = 0; i < objRootDSE.length; i++) {
    WriteToFile("Moahhh");
    var oEntrprise = objRootDSE[i];
    oComm.CommandText = "<" + oEntrprise.ADsPath + ">;(&(objectCategory=person)(objectClass=user)(givenName=a*));cn,distinguishedName;subTree";
    var oRS = oComm.Execute;

}

function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\builds\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)

FileObject.close()
}

2 个答案:

答案 0 :(得分:1)

在JScript中,您需要使用枚举器来跳过集合的元素

var objRootDSE = GetObject('GC:');
for (var childs = new Enumerator(objRootDSE) ; !childs.atEnd(); childs.moveNext()){
    var child = childs.item();
    WScript.Echo( child.Name );
};

答案 1 :(得分:0)

感谢您提出的建议 - 回答我无法解决的不可能的循环,但我现在找到了一种直接向用户查询全局目录而无需循环的方法:

var aoi = WScript.CreateObject("ADSystemInfo");
var gcBase = aoi.ForestDNSName;

var ado = WScript.CreateObject("ADODB.Connection");
ado.Provider = "ADSDSOObject";
ado.Open;
WriteToFile(aoi.ForestDNSName);
var objectList =   ado.Execute("<GC://" + gcBase + ">;(&(objectCategory=person)(objectClass=user)("+keyname+"="+keyvalue+"*));cn,distinguishedName;subTree");

if(!objectList.EOF)
{
    WriteToFile(objectList("distinguishedName").value);
}

function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)

FileObject.close()
}

再也不会了。 无论谁发现这一点,我都会帮助Google访问 - Active Directory的全局编目JavaScript查询!