【问题标题】:send mail attachment powershell发送邮件附件PowerShell
【发布时间】:2018-04-17 00:51:23
【问题描述】:

我正在为我的 Cyber​​Sec 入门课程做一个项目(奖励积分),我有一个 cmd 脚本来在 USB 驱动器中创建一个包含信息的文件我试图使用 powershell 将电子邮件发送给自己但不断收到这个错误:

At D:\mail.ps1:6 char:16
+ $att= $USBDRIVE\file.zip
+                ~~~~~~~~~
Unexpected token '\file.zip' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

这是我的完整的 powershell 脚本。

$USBDRIVE= Get-WMIObject Win32_Volume | ? { $_.label -eq 'ARNOLDHMW' } | 
select -expand driveletter

Compress-Archive -Path $USBDRIVE\file -DestinationPath $USBDRIVE\file.zip

$email = "*"

$pass = "*"

$att= $USBDRIVE\file.zip

$smtpServer = "smtp.gmail.com"

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSSl = $true
$msg.From = "$email"
$msg.To.Add("$email")
$msg.IsBodyHTML = $true
$msg.Subject = "Files Captured"
$msg.Body = "
</br>
Hi there
"
$msg.Attachments.Add($att)
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$email", 
"$pass");
$smtp.Send($msg)

由于显而易见的原因,电子邮件和密码被空白 我在这里不知所措。提前感谢您的帮助!

【问题讨论】:

  • 欢迎来到 SO。请不要将错误发布为图片,而是以文字形式发布。我们想复制和粘贴东西!
  • 好的,抱歉。
  • 关于引用字符串的主题,我建议阅读about_quoting_rules,因为这可能是您在学习 powershell 时会遇到的问题。

标签: powershell email smtp


【解决方案1】:

我很确定您遇到的问题是因为您传递给分配 $att 的值不是字符串值。尝试将变量分配为字符串

$att = "$USBDrive\File.zip"

或者更好

$att = Join-Path -Path $USBDrive -ChildPath "file.zip"

【讨论】:

  • 非常感谢!它总是吸引我的小语法!
猜你喜欢
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 2016-09-07
  • 2015-12-24
相关资源
最近更新 更多