从多个文本文件中读取并通过电子邮件发送给收件人

Read from multiple text files and email to recipient

我是 powershell 的新手,所以如果不清楚,请原谅我的措辞。

我有 powershell 命令,可以读取文本文件中的一行并将其通过电子邮件发送给收件人。 正如您在下面的代码中看到的那样,它正在寻找一个包含 "ama" 的文件 - 它确实很好。

但是,有时目录中有多个文件名中包含"ama"的文件。我希望它为名称中包含 "ama" 的每个文件发送一封电子邮件。每个文件的单独电子邮件,或目录中所有 "ama" 文件中读取同一行的摘要。

这可以做到吗?

非常感谢。

$a=Get-Content -Path "S:\Vecta\Test\*ama.*" | select -first 1 -skip 9 
$a = $a.substring(3,16)
$b = "Imported"
$msg.Body=$a

#$msg.Body=Get-Content -Path "S:\Vecta\Test\*ama.*" | select -first 1 -skip 9 |  Out-String 

#Message Subject
$msg.Subject=$b

$smtp.Send($msg)
#$attachment.Dispose();
$msg.Dispose();

编辑:

设置:

PS S:\vecta\test> "testa" >aama.txt
PS S:\vecta\test> "testb" >bama.log
PS S:\vecta\test> "testc" >cama.csv
PS S:\vecta\test> ls


    Directory: S:\vecta\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       08.03.2016     19:11             16 aama.txt
-a----       08.03.2016     19:11             16 bama.log
-a----       08.03.2016     19:11             16 cama.csv

PS S:\vecta\test> $files = Get-ChildItem -Path "S:\Vecta\Test\*ama.*"
PS S:\vecta\test> $files.count
3

PS S:\vecta\test> foreach($file in $files){$a = get-content $file; "$file=$a"}
S:\Vecta\Test\aama.txt=testa
S:\Vecta\Test\bama.log=testb
S:\Vecta\Test\cama.csv=testc

如您所见,代码有效。

只需遍历文件:

$files = Get-ChildItem -Path "S:\Vecta\Test\*ama.*" 

foreach($file in $files)
{
    $a = Get-Content $file | select -first 1 -skip 9 

    # your email logic

}