2016-03-08 38 views
0

我很新的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(); 

回答

0

编辑:

设置:

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 

} 
+0

谢谢,我已经试过,但得到follwing错误:获取内容:无法绑定参数参数“路径”,因为它是空的。 在C:\ Scripts \ AmaEmail.ps1:27 char:21 + $ a = Get-Content <<<< $ file | select -first 1 -skip 9 + CategoryInfo:InvalidData:(:) [Get-Content],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand – JMar

+0

它的工作原理,请参阅我的示例。你一定做错了什么。 – megamorf

+0

不知道为什么它昨天不行,但它正在做我现在想要的。谢谢! – JMar

相关问题