powershell - 如何将文件添加到变量 Powershell
问题描述
除了我在这里所做的:
我想发送一封只包含传输文件的邮件,但我遇到了一个小问题。为此,我$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 ...
你能帮我解决这个问题吗?
如果他们有什么不清楚的地方,请告诉我,非常感谢你的帮助
解决方案
假设您的变量$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 />')
推荐阅读
- spring-boot - 尝试部署需要静态 IP 的 PCF Spring Boot 应用程序
- c++ - 作为右值引用传递的对象在尝试应用装饰器模式时会导致错误
- typescript - Nativescript 代码共享项目中忽略 tsconfig 文件?
- vb.net - itextsharp:禁用 pdf 的剪贴板
- javascript - 麦克风输入的 p5.js 傅立叶变换没有输出
- android - Xamarin.Forms 后台/前台服务最佳做法?
- android - 如何使用毕加索库加载远程 svg 文件
- node.js - 如何检查并更新 node.js 中的 AWS dynamoDB 项目?
- php - 如何上传 Laravel 并将应用程序反应到像 GoDaddy 这样的托管服务器
- scala - 如何使用 sparkcontext 读取带引号的 csv