Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Array to string using newlines possible?

$
0
0

Hi All,

I have a log file with multiple lines of text.  I am attempting to send the log files contents in the body of an email using Send-MailMessage.  The -Body parameter only accepts a string though, and using Get-Content to read in the log file creates an Array.  I tried using [string]$array but the new line's aren't preserved.  I tried using $ofs = '`r`n' to no effect.

Any insight would be greatly appreciated.  Here is a snippet of the script for reference.

# Get CD-Drives and clear the media / connection state
Get-VM -Name *xxx* | 
Get-CDDrive | 
%{ 
    $LogOutput += "Removed drive from: " + $_.parent.name + ", with ISO: " + $_.ISOPath
    Set-CDDrive -CD $_ -NoMedia -Confirm:$False
 } |  
Where { $_.ISOPath -ne $Null }
# Output Log
$LogOutput | Out-File c:\Logs\$LogName.txt

# Send an email notification out
$EmailBody = Get-Content c:\Logs\$LogName.txt
$EmailString = [string]$EmailBody

Send-MailMessage -To "xxx" -From "xxx" -Body $EmailString -Subject "xxx" -SMTPServer "xxx" -Credential $Credential

Sample of the C:\Logs\xxx.txt file below:

Removed drive from: xxx, with ISO: [xxx] xxx.iso
Removed drive from: xxx2, with ISO: [xxx] xxx2.iso

How the current email body appears:

Removed drive from: xxx, with ISO: [xxx] xxx.iso Removed drive from: xxx2, with ISO: [xxx] xxx2.iso


Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>