为什么powershell veriable输出不正确

时间:2018-01-10 12:30:03

标签: string powershell

我正在尝试创建一个快速脚本,它将自动执行电子邮件过程的通用部分。该脚本完全适用于它的目的。但是,它不会输出任何主题的电子邮件。但是在输出的$ body中,它输出正确。为什么程序运行正常但不会添加到主题中?

此输出的唯一错误如下:

  

param:术语“param”未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$type = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the ticket type", " ")

param(
    $body = "Hi

Could this ticket please be updated; 

$ticketnumber - $company 
$timerange - $contacttype 
$bodymessage - $type

Thanks,
"
)  

# Functions

function Send-Email (
    $recipientEmail = "email",
    $subject = $(Throw "Ticket" + $type),
    $body
) {
    $outlook = New-Object -ComObject Outlook.Application
    $mail = $outlook.CreateItem(0)
    $mail.Recipients.Add("email")
    $mail.Subject = $subject
    $mail.Body = $body   # For HTML encoded emails
    $mail.Send()
    Write-Host "Email sent!"
}   

2 个答案:

答案 0 :(得分:0)

$subject = $(Throw "Ticket" + $type)

应该阅读

$subject = "Ticket" + $type

答案 1 :(得分:0)

我很困惑为什么你在那里有param和一个功能。通过将$subject包装在一个函数中,您可以使实际发送的电子邮件无法访问。此外,我建议尽可能使用Send-MailMessage。无论哪种方式,删除参数和功能,你应该很高兴去。

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$type = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the ticket type", " ")


$body = "Hi 

Could this ticket please be updated; 

$ticketnumber - $company 
$timerange - $contacttype 
$bodymessage - $type

Thanks,
"

$recipientEmail = "email"
$subject = "Ticket" + $type


{
$outlook = New-Object -comObject  Outlook.Application 
$mail = $outlook.CreateItem(0) 
$mail.Recipients.Add("email") 
$mail.Subject = $subject 
$mail.Body = $body   # For HTML encoded emails  
$mail.Send() 
Write-Host "Email sent!"
} 

修改

是否可以尝试用Send-MailMessage替换整个Outlook块,看看是否有同样的问题?

Send-MailMessage -to "email" -from "OtherEmail" -Subject $subject -Body $body -SmtpServer "SMTPServer"