Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to fetch groups and users on a win2k system

by Ananda (Pilgrim)
on Oct 11, 2002 at 05:04 UTC ( [id://204411]=perlquestion: print w/replies, xml ) Need Help??

Ananda has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am keen to know how to fetch user's as well as group list from a win2k system. I tried using win32::NetAdmin package but there seems to be a limitation to its functionality wrt win2k. Kindly advise Thanks Ananda
  • Comment on How to fetch groups and users on a win2k system

Replies are listed 'Best First'.
Re: How to fetch groups and users on a win2k system
by AcidHawk (Vicar) on Oct 11, 2002 at 06:08 UTC

    The code to list Users and Groups on a W2k

    #! perl.exe use strict; use warnings; use Win32::AdminMisc; my (@users, @groups); print "List of USERS\n"; Win32::AdminMisc::GetUsers("","",\@users); foreach my $usr(@users) { print "$usr\n"; } print "\nList of GROUPS\n"; Win32::AdminMisc::GetGroups("",GROUP_TYPE_ALL,\@groups); foreach my $grp(@groups) { print "$grp\n"; }

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      Hello AcidHawk, The code is really helpful. but again i have a problem in getting the latest Adminmisc package (2000... ver). can u help . mau be u can mail me this package at anandtirtha@samyog.com it would be very helpful. Thanks and Regards, Ananda
Re: How to fetch groups and users on a win2k system
by AcidHawk (Vicar) on Oct 11, 2002 at 05:55 UTC

    You can try AdminMisc found at Roth.net it has the following funtions

    GetGroups( $Server, $GroupType, < $List | \@List | \%List > [, $Prefi +x ] ) This will, if successful, populate the @List array with the names +of the user groups that match the group type $GroupType. If $Prefix is p +assed in then only groups with names beginning with $Prefix (case insensiti +ve) will be stored into the array. $Server is either: - The name of the machine the accounts reside on in either "\\\s +erver" or "//server" format. - The name of a domain as in "Accounting" where the user account +s reside. - An empty string ("") which returns the user accounts for the l +ocal machine.

    GetUsers( $Server, $UserPrefix, < $List | \@List | \%List > ) This will, if successful, populate the @List array with the names +of the user accounts which begin with $UserPrefix. $UserPrefix can be an empty string in which all user accounts will + be returned. This parameter is case insensitive. $Server is either: - The name of the machine the accounts reside on in either "\\\s +erver" or "//server" format. - The name of a domain as in "Accounting" where the user account +s reside. - An empty string ("") which returns the user accounts for the l +ocal machine. The third parameter can either be a scalar, an array reference or +a hash reference. If it is an array then upon success the group names populate the array. + If it is a scalar then it is converted into a hash reference. Hash references are popula +ted with the user id, full name, comment, and account flags. Returns: The number of group names stored in the array or hash.

    I havent finished putting any code together for this yet but am busy with it and will post it as I have it.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: How to fetch groups and users on a win2k system
by Schuk (Pilgrim) on Oct 11, 2002 at 08:02 UTC
    You can also differentiate between Local and Global groups by just changing
    #!/usr/bin/perl #coughcough use strict; use warnings; Win32::AdminMisc::GetGroups($Domain,GROUP_TYPE_ALL,\@Groups) Win32::AdminMisc::GetGroups($Domain,GROUP_TYPE_LOCAL,\@Groups) Win32::AdminMisc::GetGroups($Domain,GROUP_TYPE_GLOBAL,\@Groups)
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to fetch groups and users on a win2k system
by enigmae (Pilgrim) on Oct 11, 2002 at 16:10 UTC
    Greetings Ananda,
    I have done this with success using the ADSI (Active Directory Service Interfaces) which is related to the WMI (Windows Management Instrumentation) which is really usful in getting all kinds of information computer, user, group, domain.
    Here is a link on MSDN that shows many possabilities:
    ASDI Scripts
    Note that these are most likley written in VBScript, but that is pretty easy to convert to perl. Here is a Node I originally wrote to get the members of a group, Check the reply by Kanji at the bottom, it is a good start
    http://perlmonks.org/index.pl?node_id=148262
    If you have any questions don't hesitate to ask, The script I ended up writing in that node has to query a windows 2k terminal-server farm and collect processor usage stats for all users logged in.
    In unix this wouldn't be an issue, processor usage is easy, but windows had no easy way, until i had to write a c++ console program (basically an equivalent ps command) that worked on terminal servers then i could collect the stats. I found that ADSI is a wonderful resource for scripting in windows, and if by chance you don't need perl for anything other then getting groups, the VBScript library on that site might be useful enough for you. What would be handy is to make a CPAN module for translating VBScript.
    Good Luck,
    Enigmae
Re: How to fetch groups and users on a win2k system
by OzzyOsbourne (Chaplain) on Oct 11, 2002 at 17:59 UTC

    Admin::Misc won't pull correct info on a 2000 domain even in mixed mode. It will pull data from the PDC emmulator, but if you have users that were created in AD, you won't get them. You have to access ADSI directly.

    I did this in a script found here. Pull out the first 2 subs. One will do NT4, and the other will do win2k via ADSI. The ADSI stuff was kind of hard to figure out, as there isn't a lot of info out there on Perl vs. ADSI.

    -OzzyOsbourne

Re: How to fetch groups and users on a win2k system
by czrdup (Acolyte) on Oct 12, 2002 at 03:40 UTC
    Here is an example of how you would get the users and groups for a win2k domain (or computer if that is what you want). You can do strange things like figure out what users are in what groups, but that seems to be covered in some of the other links.
    #!/usr/bin/perl -w use strict; use warnings; use Win32; use Win32::OLE 'in'; my @users = &getUsers(); print "Users: \n"; print "\t", join("\n\t", @users) , "\n"; my @groups = &getGroups(); print "Groups: \n"; print "\t", join("\n\t", @groups) , "\n"; sub getGroups { my $domainurl = "WinNT://" . ( shift || Win32::NodeName ); my $domain = Win32::OLE->GetObject($domainurl) || die( Win32::OLE->LastError . "\n"); $domain->{Filter} = ["group"]; my @groups; for my $group ( in $domain ) { $groups[@groups] = $group->name; } return @groups; } sub getUsers { my $domainurl = "WinNT://" . ( shift || Win32::NodeName ); my $domain = Win32::OLE->GetObject($domainurl) || die( Win32::OLE->LastError . "\n" ); $domain->{Filter} = ["user"]; my @users; for my $user ( in $domain ) { $users[@users] = $user->name; } return @users; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://204411]
Approved by jarich
Front-paged by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found