【问题标题】:Credentials are Prompting for Send-MailMessage in PowerShell Script凭据在 PowerShell 脚本中提示 Send-MailMessage
【发布时间】:2019-04-08 13:43:16
【问题描述】:

我对@9​​87654322@ 很陌生。

我已经完成了一个PowerShell 脚本来通过电子邮件发送某些文件。我注意到每次运行脚本时都会提示我输入凭据。我进一步了解到,除非结果是错误,否则我必须输入确切的内容。

我已在 Windows 10 中将 PowerShell 脚本设置为任务计划程序 机器,但尽管以具有本地用户的身份登录,它仍然提示 管理员权限;我还给了 Run with Highest Privileges 在任务计划程序中创建任务时。

我正在尝试使用 Active Directory 从 Office 365 服务发送 Azure 中的用户。

我的目标是避免使用PowerShell 脚本来提示输入凭据。请帮帮我。

我的代码:

#smtp Credential
$smtpKey = ConvertTo-SecureString "[My Password]" -AsPlainText -Force
$smtpCredential = New-Object System.Management.Automation.PSCredential ("[MyUserName]", $smtpKey)

#Credentials are prompted here
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential smtpCredential -Attachments $filePath  

更新:

正如@Lee_Dailey 在他的回答中指出的那样,我错过了变量smtpCredential$,我在其中调用了Send-MailMessage 任务

【问题讨论】:

  • 你离开了告诉 PoSh“这是一个变量”的$。 [咧嘴]
  • @Lee_Dailey 这么小的错误让我付出了很多!谢谢你指出。指定您的评论作为答案,我将确认相同。再次感谢您!
  • 考虑使用splatting 避免冗长的命令,容易忽略错误。
  • @hiFI - 完成!很高兴你让它工作了...... [grin]
  • @lotPings 非常感谢您的回复,splatting 对我来说绝对是此时此刻值得研究的宝贵东西

标签: windows powershell office365


【解决方案1】:

你的错误是由于缺少 $ 来告诉 PoSh smtpCredential 是一个变量。 [咧嘴一笑]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2016-09-06
    • 2015-02-16
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多