Link to home
Start Free TrialLog in
Avatar of 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  


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


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

$server = "Server001"
$SMTPserver = ""
$from = ""
$to = ""
$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={$}} | 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)

Open in new window

Avatar of slidingfox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sterlingdev


Excellent, thank you, worked perfectly!
Fast, simple and working reply