尝试使用在后台发送邮件start-job
,但在向脚本块传递参数时遇到问题。
邮件参数定义:
$mailParams=@{
To = $AddressTo
From = $AddressFrom
Subject = $Subject
Body = $Body
SMTPServer = $SMTPServer
#BodyAsHTML = $True
#Port = 587
#UseSSL = $True
#Credential = $mailCred
}#End mailParams
发送邮件命令:
Send-MailMessage @mailparams
预期的:
start-job -scriptblock {Send-MailMessage @mailparams}
尝试使用start-job -scriptblock {Send-MailMessage} -ArgumentList $mailparams
,但同样的问题,所有参数都为空
顺便说一句,欢迎提出有关如何在后台执行它的更好建议
答案1
你需要param()
在你的 Scriptblock 中有一个来接收你发送的参数ArgumentList
$MailParams = @{
To = $AddressTo
From = $AddressFrom
Subject = $Subject
Body = $Body
SMTPServer = $SMTPServer
#BodyAsHTML = $True
#Port = 587
#UseSSL = $True
#Credential = $mailCred
}
Start-Job { param($MailParams) ; Send-MailMessage @MailParams } -ArgumentList $MailParams
我总是尝试将脚本块视为函数。在函数中,您还需要传递和接收参数/参数。脚本块也是如此。