1

我知道如何使用 cat 文件

cat file.txt

但我有变量

[string]$body = "Body message here"

我需要帮助更改它以从中获取文本内容file.txt

我试过了,但没有用。

  [string]$body = $cat file.txt

这是完整的脚本,我正在尝试更改param部分。

param 
(        
    [string]$email = $(read-host "Enter a recipient email"),
    [string]$subject = $(read-host "Enter the subject header"), 
    [string]$body = $(read-host "Enter the email text (optional)")
)

param 
(        
    [string]$email = "my@email.com",
    [string]$subject = "server", 
    [string]$body = gc C:\Users\myuser\Documents\somefolder\GnuWin32\bin\status.txt
)

# ==========================================================================
#   Functions
# ==========================================================================

function Send-Email
(
    [string]$recipientEmail = $(Throw "At least one recipient email is required!"), 
    [string]$subject = $(Throw "An email subject header is required!"), 
    [string]$body
)
{
    $outlook = New-Object -comObject  Outlook.Application 
    $mail = $outlook.CreateItem(0) 
    $mail.Recipients.Add($recipientEmail) 
    $mail.Subject = $subject 
    $mail.Body = $body

    # For HTML encoded emails 
    # $mail.HTMLBody = "<HTML><HEAD>Text<B>BOLD</B>  <span style='color:#E36C0A'>Color Text</span></HEAD></HTML>"

    # To send an attachment 
    # $mail.Attachments.Add("C:\Temp\Test.txt") 

    $mail.Send() 
    Write-Host "Email sent!"
}

Write-Host "Starting Send-MailViaOutlook Script."

# Send email using Outlook
Send-Email -recipientEmail $email -subject $subject -body $body

Write-Host "Closing Send-MailViaOutlook Script."
4

2 回答 2

3

尝试以下

[string]$body = gc file.txt

该命令gc是一个别名Get-Content,它将获取指定项目的内容并将其作为string

编辑

正如所C.B.指出的,在您的示例中,您尝试使用$cat而不是cat. $cat试图引用一个未定义但是cat另一个别名的变量Get-Content

编辑2

看起来您正在尝试初始化参数。如果是这样,您需要执行以下操作

[string]$body = $(gc "C:\Users\myuser\Documents\somefolder\GnuWin32\bin\status.txt")
于 2013-03-15T15:48:23.070 回答
2

改变:

[string]$body = ( gc C:\Users\myuser\Documents\somefolder\GnuWin32\bin\status.txt )

注意括号。

于 2013-03-15T16:00:15.217 回答