Powershell将文件作为邮件附件发送

rsc*_*rin 2 powershell sendmail powershell-2.0

我需要检查文件是否存在于路径中.

如果是,我需要将其作为邮件附件发送.

我的代码现在:

$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 pluto@pippo.com -From preg_new_stream@pippo.com -Subject "test mail" -Body "file name is: $new_file"
}else{
        echo false
}
Run Code Online (Sandbox Code Playgroud)

alr*_*roc 6

Send-Mailmessage 接受要附加的文件路径.

Send-MailMessage -SmtpServer xxxxxxxxxx -To pluto@pippo.com -From preg_new_stream@pippo.com -Subject "test mail" -Body "file name is: $new_file" -attachment $new_file
Run Code Online (Sandbox Code Playgroud)