0

In addition to what I did here:

Loop Through excel file using Powershell

I want to send a mail with only the files that are transfered, and I got a small problem with that. To do that and foreach file transfered I added the file to a variable $FilenameFR with the script below.

Here is what I tried

#Look for each number in the columns selected    
    for($j=1; $j -le $rowMax-1; $j++){
        
        $Noria = $sheet.cells.Item($RowNoria+$j, $colNoria).text
        $NheSN = $sheet.cells.Item($RowNheSN+$j, $colNheSN).text
        $Site = $sheet.cells.Item($RowSite+$j, $colSite).text
            if ($Noria -like $SBF -and $NheSN -eq $SN) {  
                Write-Host ($Noria)
                Write-Host ($Site)
                write-Host ($NheSN)
                
                If ($Site -eq "TMF" -or $Site -eq "TSF") {
                        Copy-item "P:\MK_M\$F" -Destination "\\inshare.collab.group.safran\COM\NoriaC\CoC\Test_MK_D\France\"
                        $FR = $FR + 1 #coutn how many files I transfered
                        $FilenameFR += $F #here for every file transfered i add it's name to this variable

But when I call $FilenameFR in my mails with this script

$Mail.Body = 
"Hello,

You've received $FR files, if you Could please treat the certificates received on your folder in the next few days it would be very good.

Please find the link to the folder below :

....

Thank you for your understanding and have a nice day.

PS. The files are :
"
         foreach ($File in $FilenameFR) {     # Cette boucle c'est pour avoir un nom de Fichier par ligne dans le mail
         $Mail.Body += "$File"
         }
        $Mail.Send()   
       }

Instead of getting each file in line a got them one attached to the other.

exemple : File1.pdfFile2.pdfFile3.pdf ...

Can you please help me with that.

If their is anything not clear please tell me and thank you so much for your help

1
  • 1
    Change += "$file" to += "${file}`n" Commented Sep 2, 2021 at 9:03

1 Answer 1

3

Assuming your variable $F has the filename for each iteration and variable $FR is set to zero before the major loop, you do not want to add the filenames with += to an (up to then) undefined variable.
Instead, capture the filenames as string array and join these later with newlines in the email.

$FR          = 0  # initialize the counter to 0
$source      = 'P:\MK_M'
$destination = '\\inshare.collab.group.safran\COM\NoriaC\CoC\Test_MK_D\France'

# loop through the files
$FilenameFR = for ($i = 0; $i -lt $filename.Count; $i++) {
    $F     = $filename[$i].Name
    $SN    = $F.split("_")[0]
    $split = $F.split("_")[1]
    $SB    = -join $split[-3..-1]
    $SBF   = "*_*$SB`_*"

    # Look for each number in the columns selected    
    for ($j = 1; $j -le $rowMax-1; $j++) {    
        $Noria = $sheet.cells.Item($RowNoria + $j, $colNoria).text
        $NheSN = $sheet.cells.Item($RowNheSN + $j, $colNheSN).text
        $Site  = $sheet.cells.Item($RowSite  +$j, $colSite).text
        if ($Noria -like $SBF -and $NheSN -eq $SN) {  
            Write-Host "$Noria`r`n$Site`r`n$NheSN"
            if ($Site -eq "TMF" -or $Site -eq "TSF") {
                Copy-Item -Path (Join-Path -Path $source -ChildPath $F) -Destination $destination
                # output the filename so it gets collected in variable $FilenameFR
                $F
                $FR++   # increment the file counter
            }
        }
    }
}

You now have an array of filenames in variable $FilenameFR and to use that in the mail body you can do:

$Mail.Body = "@
Hello,

You've received $FR files, if you Could please treat the certificates received on your folder in the next few days it would be very good.

Please find the link to the folder below :

....

Thank you for your understanding and have a nice day.

PS. The files are :

%%TRANSFERREDFILES%%
@" -replace '%%TRANSFERREDFILES%%', ($FilenameFR -join [environment]::NewLine)

$Mail.Send()   

Of course, if this mail is to be sent in HTML format, you need to join the filenames witn <br /> instead of [environment]::NewLine:

-replace '%%TRANSFERREDFILES%%', ($FilenameFR -join '<br />')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you as well @Theo you did not only give me an answer to my question but you helped me with all my script, Thank you again !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.