You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本邮件主题不显示且报param未识别错误求助

Fixing Your PowerShell Email Script Issues

Hey there, let's work through the two problems in your script: the param error and the missing email subject.

1. Why you're seeing the param not recognized error

In PowerShell, the param() block must be the first executable code in your script—you can't run other commands (like loading assemblies or showing input boxes) before it. Your current code runs [System.Reflection.Assembly]::LoadWithPartialName() and pops up an InputBox before the param() block, which breaks PowerShell's syntax rules. That's why you get the "term 'param' is not recognized" error.

2. Why the email subject isn't showing up

On top of the param placement issue, your Send-Email function has a critical mistake in the $subject parameter:

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

The Throw keyword is used to intentionally trigger an error, not assign a value. This line would crash the function as soon as it runs, and even if it didn't, it's not setting the subject text you want. Your goal is to set the subject to "Ticket" + $type, so you need to remove the Throw entirely.

Additionally, since your original param() block was in the wrong place, variables like $type (from the InputBox) weren't being properly passed to the function's scope.

Corrected Script

Here's the fixed version with explanations of key changes:

# Move param() to the VERY TOP of the script (required by PowerShell)
param(
    # Default body text with placeholders
    $body = "Hi Could this ticket please be updated; $Ticketnumber - $company $Timerange - $Contacttype $Bodymessage - $type Thanks, "
)

# Load assembly and get ticket type input (now AFTER param block)
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$type = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the ticket type", "Ticket Type Input")

# Functions
function Send-Email (
    $recipientEmail = "your-recipient@example.com", # Replace with actual default email
    $subject = "Ticket - $type", # Fixed: no Throw, just set the subject text
    $body
) {
    $outlook = New-Object -ComObject Outlook.Application
    $mail = $outlook.CreateItem(0)
    
    # Add recipient (use the parameter instead of hardcoding)
    $mail.Recipients.Add($recipientEmail)
    $mail.Subject = $subject
    $mail.Body = $body
    
    $mail.Send()
    Write-Host "Email sent successfully to $recipientEmail!"
}

# Don't forget to call the function! (You were missing this in your original code)
Send-Email -body $body

Key Fixes:

  • Moved param() to the start of the script to comply with PowerShell syntax
  • Removed Throw from the $subject parameter and set it to your intended text ("Ticket - $type")
  • Made the recipient email use the function parameter instead of hardcoding (easier to reuse)
  • Added a call to Send-Email at the end (your original code defined the function but never ran it)
  • Added a label to the InputBox for clarity

Final Notes

  • Make sure variables like $Ticketnumber, $company, etc., are defined somewhere in your script (they're referenced in the $body but missing in your original code)
  • Avoid using LoadWithPartialName—it's deprecated. Use Add-Type -AssemblyName Microsoft.VisualBasic instead for newer PowerShell versions

内容的提问来源于stack exchange,提问作者Matthew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:50:10