2013-04-10 48 views
2

我需要檢查文件是否存在路徑中。Powershell發送文件作爲郵件的附件

如果是,我需要將它作爲郵件的附件發送。

這是我現在的代碼:

$file='STREAM_preg.txt' 
$pathfile ='c:\Users\root\Desktop' 
$dest ='C:\Users\root\Desktop\test' 

cd $pathfile 
if(Test-Path $file){ 
     $datestamp = get-date -uformat "%Y%m%d%H%M" 
    Copy-Item $file $dest\$datestamp"_"$file 
    $new_file = echo $datestamp"_"$file 
    echo $new_file 
     # Send-MailMessage -SmtpServer xxxxxxxxxx -To [email protected] -From [email protected] -Subject "test mail" -Body "file name is: $new_file" 
}else{ 
     echo false 
} 

回答

6

Send-Mailmessage接受要附加的文件路徑。

Send-MailMessage -SmtpServer xxxxxxxxxx -To [email protected] -From [email protected] -Subject "test mail" -Body "file name is: $new_file" -attachment $new_file 
+1

只是爲了完整性: -Attachments的String [] 文件的路徑和文件名要附加到電子郵件。使用此參數或管道路徑/文件名稱。 – ArchGriffin 2013-04-10 23:41:30

0

發送,需要MAILMESSAGE一個或多個文件路徑作爲附着物管道輸入。只需將文件路徑傳送到send-meailmessage命令中,並將其作爲附件發送到電子郵件。

相關問題