开发者

Input file for Perl job

开发者 https://www.devze.com 2023-03-10 01:01 出处:网络
I have a simple batch job that looks up users in a group from Active Directory. If the user count exceeds a threshold, it sends an email. I included a stripped down version of the code below just to g

I have a simple batch job that looks up users in a group from Active Directory. If the user count exceeds a threshold, it sends an email. I included a stripped down version of the code below just to give you an idea of what i am attempting to accomplish. The thing i need help with is figuring out how to populate the $Group and $t values (currently hard coded) with values from another file. Not sure if i should use a simple log file or xml but the other file with have a list of group names and the threshold开发者_如何学编程 of the number of users we should have for each group.

  • Security_Group_X 50
  • Security_Group_Y 40

I then want this job to read those values from the input file and do a big For Each statement. Not sure what the input file should be formatted like and the easiet way to read the file to make it process the code below for each group in the file.

    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";
    }
    }

Thanks in advance for any advice.


Here was my solution:

Sample of config.txt. Just a plain tab delimited file with 2 values in each line:

  • Security_Group_X 50
  • Security_Group_Y 40

Sample of code:

$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
}


I think you may be looking to set up a configuration file.

Have a look under the Config:: name-space on cpan.

Here's one possible solution thats based on Config::Auto. I've chosen to format the configuration file as YAML.

test program test.pl:

#!/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";
}

contents of configuration file test.config:

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

This produces:

---
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

Update: test.config could just as easily contain XML:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号