2011-06-01 58 views
1

我有一个简单的批量作业,它从Active Directory中查找组中的用户。如果用户数超过阈值,它会发送一封电子邮件。我在下面包含了一个精简版的代码,只是为了让你知道我正在尝试完成什么。我需要帮助的事情是弄清楚如何用另一个文件中的值填充$ Group和$ t值(当前是硬编码的)。不知道我是否应该使用简单的日志文件或xml,但是具有组名列表的其他文件以及我们应该为每个组使用的用户数的阈值。Perl作业的输入文件

  • Security_Group_X 50
  • Security_Group_Y 40

然后我想要这份工作来读取输入文件中的值,做一个大For Each语句。不知道输入文件应该如何格式化,以及读取文件的方式,以便为文件中的每个组处理下面的代码。

my $Group = "Security_Group_X"; 
    Win32::NetAdmin::GetDomainController('',$Domain,$Server); 

    if(! Win32::NetAdmin::GroupGetMembers($Server,$Group,\@UserList)){ 
print "error connecting to group " . $Group; 
    } 
    else { 
$i=0; 
$t=50; 

foreach $user (@UserList){ 
      $i++. 
      print " $user\n"; 
     } 
    print $i . " Current users in this group.\n"; 

    if ($i > $t){ 
    ### i have some code here that would email the count and users ### 
    } 
    else { 
    print $Group . " is still under the limit. \n"; 
    } 
    } 

在此先感谢您的任何建议。

回答

0

我想你可能正在设置一个配置文件。

看看cpan上的Config :: name-space。

下面是基于Config::Auto的一种可能的解决方案。我选择将配置文件格式化为YAML。

测试程序test.pltest.config配置文件的

#!/usr/bin/perl 
use common::sense; 

use Config::Auto; 
use YAML; 

my $config = Config::Auto::parse(); 

print YAML::Dump {config => $config}; 

my %groups = %{ $config->{groups} || {} }; 

print "\n"; 

foreach my $group_name (sort keys %groups) { 
    my $group_limit = $groups{$group_name}; 

    print "group name: $group_name has limit $group_limit\n"; 
} 

内容:

--- 
# Sample YAML config file 
groups: 
    Security_Group_X: 50 
    Security_Group_Y: 40 

这将产生:

--- 
config: 
    groups: 
    Security_Group_X: 50 
    Security_Group_Y: 40 

group name: Security_Group_X has limit 50 
group name: Security_Group_Y has limit 40 

更新:test.config可能只是EASI LY包含XML:

<config> 
    <!-- Sample XML config file --> 
    <groups> 
    <Security_Group_X>50</Security_Group_X> 
    <Security_Group_Y>40</Security_Group_Y> 
    </groups> 
</config> 
+0

我无法使用配置或YAML。猜猜我的盒子上的那些晚上。我决定做一些更简单的事情。只是一个普通的制表符分隔的文本文件。然后使用一个分割并将该行中的每个值分配给其自己的变量以供使用。 – Gabriel 2011-06-02 19:37:08

+0

够公平的。为了更好地回答这个问题,我还为我的答案添加了一个XML版本的配置。如果你有'XML :: Simple',你可以加载它,例如使用XML :: Simple;我的$ config = XMLin('test.config');' – dwarring 2011-06-03 03:34:49

1

这里是我的解决办法:

样品的config.txt的。只是一个普通的标签中的每一行分隔的文件用2个值:

  • Security_Group_X 50
  • Security_Group_Y 40

样品的代码:

$CONFIGFILE = "config.txt"; 
open(CONFIGFILE) or die("Could not open log file."); 
foreach $line (<CONFIGFILE>) { 

@TempLine = split(/\t/, $line); 

$GroupName = $TempLine[0]; 
$LimitMax = $TempLine[1]; 

    # sample code from question (see question above) using the $GroupName and $LimitMax values 
}