I want to extract certain attributes (such as email, phone, etc.) of an active directory users using Node JS. According to this documentation, I was able to extract the attributes of a certain user using this piece of code:
var ActiveDirectory = require('activedirectory');
var ad = new ActiveDirectory({ url: 'ldap://domain.com',
baseDN: 'dc=domain,dc=com',
username: 'user@domain.com',
password: 'password',
attributes: {
user: [ 'givenName', 'mail', 'mobile' ],
// group: [ 'anotherCustomAttribute', 'objectCategory' ]
}
});
var sAMAccountName = 'desiredUsername';
ad.findUser(sAMAccountName, function(err, user) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! user) console.log('User: ' + sAMAccountName + ' not found.');
else console.log(JSON.stringify(user));
});
Now I want to know how I can extract the desired attributes of all active directory users, considering the fact that some users don't have a groupName.
Is it possible to extract all existing sAMAccountName in the active directory and extract the attributes of each user this way?
Use findUsers instead of findUser.
Don't include a filter, and it'll use the default filter of finding all users:
ad.findUsers(function(err, users) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if ((! users) || (users.length == 0)) console.log('No users found.');
else {
console.log('findUsers: '+JSON.stringify(users));
}
});