Avatar of sterlingdev
sterlingdev
 asked on

Using a text file as the body of an email in Powershell

Hi, I have a script currently that produces output to a text file and then the attachment is emailed out. I would like to change the script so that it displays the text of this output in the BODY of the email.

Is this possible? The text file is just a few rows of standard text, eg...

servername               versionnumber         date  

etc.

I don't nessicarily need the text file, it was just a temporary means of producing this email on a tight deadline!

Thanks

if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSsnapin VMware.VimAutomation.Core
}

$server = "Server001"
$SMTPserver = "emailserv.mydomain.co.uk"
$from = "sendermailer@mydomain.co.uk"
$to = "sterlinghd@mydomain.co.uk"
$attachments = "C:\Scripts\Generate-VMToolsVersionReport\VMWareTools_VersionReport.txt"
$subject = "VMWare Tools Report from vc001 server Cluster"
$bodytext = "Attachment contains the current versions of VMWare tools by VM on the vc001 Vcenter cluster"

# Connect to the vCenter server(s) 
$vcserver = connect-VIServer $server -Protocol https -WarningAction SilentlyContinue | Out-Null

# get the vmware tools version for each VM  
get-vm | % { get-view $_.ID } | select Name, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}} | Sort-Object toolsversion -Descending | out-file $attachments

# Disconnect from the vCenter server(s) 
Disconnect-VIServer -Server $server -Force:$true -Confirm:$false

$mailer = New-Object Net.Mail.SMTPclient($SMTPserver)
$msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $bodytext)
$Attach = new-object Net.Mail.Attachment($attachments)
$msg.Attachments.Add($attach)
$mailer.send($msg)

Open in new window

Powershell

Avatar of undefined
Last Comment
sterlingdev

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slidingfox

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sterlingdev

ASKER
Excellent, thank you, worked perfectly!
sterlingdev

ASKER
Fast, simple and working reply
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes