Get user AD groups

Get user AD groups

drew_dewit
Advocate Advocate
1,025 Views
6 Replies
Message 1 of 7

Get user AD groups

drew_dewit
Advocate
Advocate

I am looking to load lisp routines in an acaddoc based on the users Active Directory groups. Has anyone done this before? How can i connect a LISP routine to active directory.

0 Likes
1,026 Views
6 Replies
Replies (6)
Message 2 of 7

Nick_Hall
Collaborator
Collaborator

Hi

 

This isn't elegant, but it will work

 

It involves running net user /domain for the current user & parsing the output

 

It involves some temporary files - I've put everything in C:\Temp in this example

 

  • Create c:\temp\ad.bat

 

 

@Anonymous off
net user /domain %1 > c:\temp\out.txt​

 

 

  • In your lisp code call it like this

 

 

(startapp "c:\\temp\\ad.bat" (getenv "USERNAME"))​

 

 

  • That will create a text file c:\temp\out.txt
  • Open the file & read all the lines until you get to one that starts with Global Group memberships
  • From that line to the end of the file you get a list of groups 

 

 

Logon hours allowed          All

Local Group Memberships
Global Group memberships     *CADPRO-USERS         *CADPRO-MANAGE
                             *CADPRO-VPN           *Domain Users
                             *CADPRO-TECH​

 

 

  • Split each line to get the group names, which begin with a * 

You can do it more elegantly using a tool like DOSLib - https://wiki.mcneel.com/doslib/home - which will allow you to hide the DOS window

 

Hope that helps


Nick

Message 3 of 7

CodeDing
Mentor
Mentor

@drew_dewit ,

 

Using @Nick_Hall 's approach, this will get you the contents of the text file. I don't have time to mess with it much, but it's a start in the right direction.

I found that I needed to add about a 3 second pause until the file could be executed through the command prompt.

(defun c:TEST ( / fMain fOut f txt str time)
(setq fMain (vl-filename-mktemp nil nil ".bat"))
(setq f (open fMain "w"))
(write-line "@echo off" f)
(write-line (strcat "net user /domain %1 > \"" (setq fOut (vl-filename-mktemp nil nil ".txt")) "\"") f)
(close f)
(startapp fMain (getenv "USERNAME"))
(setq txt '() time (getvar 'MILLISECS))
(while (< (- (getvar 'MILLISECS) time) 3000);<-- wait 3 seconds
  ;do nothing
);while
(setq f (open fOut "r"))
(while (setq str (read-line f))
  (setq txt (cons str txt))
);while
(close f)
;Do stuff with file contents here..
(foreach str txt
  (princ "\n") (princ str)
);foreach
(princ)
);defun

Best,

~DD

0 Likes
Message 4 of 7

Sea-Haven
Mentor
Mentor

You would be better writing bat to a known directory as the files are never erased if using vl temp unless after reading does a delete.

 

 (vl-file-delete f)
(princ)
)  ; end defun
Message 5 of 7

Nick_Hall
Collaborator
Collaborator

Thanks @CodeDing  for creating a fuller solution - I have a neat & tidy version for node.js but didn't have the time to work up an Autolisp version

0 Likes
Message 6 of 7

CodeDing
Mentor
Mentor

@Sea-Haven ,

 

I have not personally tested, but according to vl-filename-mktemp documentation:

 

"All file names generated by vl-filename-mktemp during a session are deleted when you exit the application."

0 Likes
Message 7 of 7

Sea-Haven
Mentor
Mentor

Thanks for that always learning, only issue may be if you don't exit cleanly etc.but delete-file will probably not be used also.

0 Likes