Home > OS >  Send a single email with multiple data
Send a single email with multiple data

Time:04-23

I have a script that looks at overloaded tables on an application.

If the table exceeds 90% of the maximum of the table then the script sends an email with the overloaded tables.

To do that, I created a function that analyze the table :

# elemt is the current data and Max is the maximum of the table
$elemt = @($PcaDos, $PcaMal, $PcaRdv, $Pc2Tpt, $Pc2Dti, $Pc2His, $PcaLigFact)
$tableMax =  @($PcaDosMax, $PcaMalMax, $PcaRdvMax, $Pc2TptMax, $Pc2DtiMax, $Pc2HisMax, $PcaLigFactMax)

#   calculates 90% of the maximum of the table 
$total = @()
Foreach($item in $tableMax){
 $value = $item * 0.9
 $total  = $value
}

function OverloadTable{
    param(
        $variable,
        [int]$num,
        [String]$table
    )
if($Variable -gt $total[$Num]){
    $path = "The $path table is overloaded"
    $path
    echo 'sending mail' 
 }else{
    echo "The $table table is not overloaded"

 }
}

OverloadTable -Variable $PcaDos -Num 0 -Table PcaDos 
OverloadTable -Variable $PcaMal -Num 1 -Table PcaMal
OverloadTable -Variable $PcaRdv -Num 2 -Table PcaRdv
OverloadTable -Variable $Pc2Tpt -Num 3 -Table Pc2Tpt
OverloadTable -Variable $Pc2Dti -Num 4 -Table Pc2Dti
OverloadTable -Variable $Pc2His -Num 5 -Table Pc2His
OverloadTable -Variable $PcaLigFact -Num 6 -Table PcaLigFact

This function works but it sends an email for each overloaded table whereas I want it to send a single email for each overloaded table. I tried to make a list in the function but the functions don't save the previous variables. I also tried setting the elemt list to 1 for each overloaded table and 0 for the others and send mail for the tables to 1 but it doesn't work too. I'm a beginner in function. I know it's possible but maybe I'm not using the right way

CodePudding user response:

This may work for you. probably least amount of edits, but I'm sure not best way...

  1. Correct to $Table like @zett42 suggested.

  2. Remove your email execution from the function. Assuming "echo 'sending mail'" is an actual execution.

  3. Since you're expressing $Path within the function, though your could also use return $Path. Remove the echos you do not want in your Email body. Then wrap your function calls with a Subexpression Operator like this...

$(
    OverloadTable -Variable $PcaDos -Num 0 -Table PcaDos 
    OverloadTable -Variable $PcaMal -Num 1 -Table PcaMal
    OverloadTable -Variable $PcaRdv -Num 2 -Table PcaRdv
    OverloadTable -Variable $Pc2Tpt -Num 3 -Table Pc2Tpt
    OverloadTable -Variable $Pc2Dti -Num 4 -Table Pc2Dti
    OverloadTable -Variable $Pc2His -Num 5 -Table Pc2His
    OverloadTable -Variable $PcaLigFact -Num 6 -Table PcaLigFact
) | Foreach-Object { $EMAILBODY  = $_   "`r`n" }
  1. The ForEach-Object section will concatenate you a string from each function outputted $Path and will add line breaks, so remove if you don't want.
  2. Execute your email (outside of function) with $EMAILBODY.

Subexpression Operator

  • Related