2011-06-01 57 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 
}