1

I've already gotten Perl to create an array of usernames (@ua); now I need to check to see if each one exists in Active Directory. The best way I thought of to do this is to run dsquery on each user and determine if the command exits with zero or nonzero. I wrote the following:

foreach(@ua)
{
    $out = `C:\\Windows\\System32\\dsquery.exe user -samid $_`;
}

When I run this, I get a repeated list of this in the command line console:

'C:\Windows\System32\dsquery.exe' is not recognized as an internal or external command, operable program or batch file.

However, dsquery.exe is in that location, as I can prove by simply running it:

C:\verify_users>C:\Windows\System32\dsquery.exe user -samid ...
"CN=...,OU=...,OU=...,OU=...,DC=...,DC=...,DC=..."

Any thoughts?

Thanks!

4

3 に答える 3

3

Miguel が言うように、代わりに Net::LDAP を使用してください。

#!/usr/bin/perl
use warnings;
use strict;

use Net::LDAP;

my $tgt_user = shift or die "Usage: fetch_user_details <username>";

my $Server   = 'server.foo.local';
my $User     = 'user@foo.local';
my $Password = 'userpass';
my $LdapBase = 'OU=SBSUsers,OU=Users,OU=MyBusiness,DC=foo,DC=local';
# To AND conditions: "(&(cond1) (cond2))"
my $Filter   = "SAMAccountName=$tgt_user";


# Bind a connection
my $ad = Net::LDAP->new("ldap://$Server")
        or die("Could not connect to LDAP server: $Server");
my $res = $ad->bind( $User, password=>$Password );
if ($res->code) { die("Unable to bind as user $User: ".$res->error); }

# Run the search
# Could have attrs=>'a,b,c' for a search too
$res = $ad->search(base=>$LdapBase, filter=>$Filter);
if ($res->code) { die("Failed to search: ".$res->error); }

# Display results
my $count = $res->count;
print "Found $count matches\n";

for my $entry ($res->entries) {
        $entry->dump;
        # print $entry->get_value('givenname'),"\n";
}

$ad->unbind;
exit;

上記は、ドメイン名が SBS を使用した machine.foo.local のようなものであると仮定すると、ほとんどうまくいくはずです。

于 2013-01-24T18:21:57.990 に答える
3

外部コマンドを実行する必要がある場合は、システム コマンドを使用できます。

system("C:\\Windows\\System32\\dsquery.exe user -samid $_");

コマンドをより詳細に制御する必要がある場合は、次のモジュールを試してください:

しかし、本当に Active Directory に対してクエリを実行したい場合は、Net::LDAPなどの特定の CPAN モジュールを使用することをお勧めします。

于 2013-01-24T14:53:47.500 に答える
0

open出力を操作する場合は、次の関数を使用します。

open(N, "C:\\Windows\\System32\\dsquery.exe user -samid $_ |");

または、コマンドのみを実行する場合は、次のsystem関数を使用します。

system("C:\\Windows\\System32\\dsquery.exe user -samid $_");
于 2013-01-24T15:22:20.423 に答える