首页 > 解决方案 > 如何将文件添加到变量 Powershell

问题描述

除了我在这里所做的:

使用Powershell循环遍历excel文件

我想发送一封只包含传输文件的邮件,但我遇到了一个小问题。为此,我$FilenameFR使用以下脚本将文件添加到变量中并传输了 foreach 文件。

这是我尝试过的

#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

但是当我$FilenameFR用这个脚本打电话给我的邮件时

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

而不是让每个文件排成一行,而是让它们一个连接到另一个。

示例:File1.pdfFile2.pdfFile3.pdf ...

你能帮我解决这个问题吗?

如果他们有什么不清楚的地方,请告诉我,非常感谢你的帮助

标签: powershell

解决方案


假设您的变量$F具有每次迭代的文件名并且变量$FR在主循环之前设置为零,您不想将文件名添加+=到(直到那时)未定义的变量。
相反,将文件名捕获为字符串数组,然后在电子邮件中使用换行符加入这些文件名。

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

您现在在变量中有一个文件名数组$FilenameFR,并且要在邮件正文中使用它,您可以执行以下操作:

$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()   

当然,如果要以 HTML 格式发送此邮件,则需要加入文件名<br />而不是[environment]::NewLine

-replace '%%TRANSFERREDFILES%%', ($FilenameFR -join '<br />')

推荐阅读