Reputation: 3512
I know how to use cat file
cat file.txt
but I have variable
[string]$body = "Body message here"
I need help changing it to get the text content from file.txt
I tried, but it didn't work.
[string]$body = $cat file.txt
Here is full script, I am trying to change param
section.
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)")
)
to
param
(
[string]$email = "[email protected]",
[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."
Upvotes: 2
Views: 7783
Reputation: 754585
Try the following
[string]$body = gc file.txt
The command gc
is an alias for Get-Content
which will get the content of the specified item and return it as a string
EDIT
As C.B.
pointed out, in your example you are trying to use $cat
instead of cat
. $cat
attempts to refer to a variable which isn't defined but cat
is another alias for Get-Content
EDIT2
It looks like you are trying to initialize a param. If so you need to do the following
[string]$body = $(gc "C:\Users\myuser\Documents\somefolder\GnuWin32\bin\status.txt")
Upvotes: 3
Reputation: 60910
Change:
[string]$body = ( gc C:\Users\myuser\Documents\somefolder\GnuWin32\bin\status.txt )
note the brackets.
Upvotes: 3