VMware Cloud Community
Biyouk
Contributor
Contributor

Formating output to mail

Hi

I created a script and used the command Start-Transcript / Stop transcript to generate an output file.

I send the output file by email but the formatting is ugly. I'm beginner in Powershell / PowerCLI.

Could you provide me any tips how to produce a good report on an email.

Ex :

********************** Windows PowerShell Transcript Start Start time: 20121220165023 Username : xxxxxxxxxMachine      : xxxxxxxx (Microsoft Windows NT 6.1.7600.0) ********************** Transcript started, output file is D:\support\Scripts\Resource_Pool_xxxxxxx\Report.csv *** Resource Pool information of 1_OVC01_GOLD : *** Current CPU Shared = 2000 Current Memory Shared = 2000 Current number of Powered On VM Guest in this resource pool: 5 The new number of CPU and Memory Shared for this Pool is = 2000 The values of this resource pool doesn't need to be updated *** Resource Pool information of 2_OVC01_SILVER : *** Current CPU Shared = 1400 Current Memory Shared = 1400 Current number of Powered On VM Guest in this resource pool: 7 The new number of CPU and Memory Shared for this Pool is = 1400 The values of this resource pool doesn't need to be updated *** Resource Pool information of 3_OVC01_BRONZE : *** Current CPU Shared = 300 Current Memory Shared = 300 Current number of Powered On VM Guest in this resource pool: 3 The new number of CPU and Memory Shared for this Pool is = 300 The values of this resource pool doesn't need to be updated *** Resource Pool information of 1_VCS01_GOLD : *** Current CPU Shared = 18000 Current Memory Shared = 18000 Current number of Powered On VM Guest in this resource pool: 45 The new number of CPU and Memory Shared for this Pool is = 18000 The values of this resource pool doesn't need to be updated *** Resource Pool information of 2_VSC01_SILVER : *** Current CPU Shared = 6800 Current Memory Shared = 6800 Current number of Powered On VM Guest in this resource pool: 34 The new number of CPU and Memory Shared for this Pool is = 6800 The values of this resource pool doesn't need to be updated *** Resource Pool information of 3_VSC01_BRONZE : *** Current CPU Shared = 3200 Current Memory Shared = 3200 Current number of Powered On VM Guest in this resource pool: 32 The new number of CPU and Memory Shared for this Pool is = 3200 The values of this resource pool doesn't need to be updated *** Resource Pool information of 1_VCS02_GOLD : *** Current CPU Shared = 16000 Current Memory Shared = 16000 Current number of Powered On VM Guest in this resource pool: 40 The new number of CPU and Memory Shared for this Pool is = 16000 The values of this resource pool doesn't need to be updated *** Resource Pool information of 2_VSC01_SILVER : *** Current CPU Shared = 6400 Current Memory Shared = 6400 Current number of Powered On VM Guest in this resource pool: 32 The new number of CPU and Memory Shared for this Pool is = 6400 The values of this resource pool doesn't need to be updated *** Resource Pool information of 3_VCS02_BRONZE : *** Current CPU Shared = 3100 Current Memory Shared = 3100 Current number of Powered On VM Guest in this resource pool: 31 The new number of CPU and Memory Shared for this Pool is = 3100 The values of this resource pool doesn't need to be updated ********************** Windows PowerShell Transcript End End time: 20121220165027 **********************

Thanks

0 Kudos
6 Replies
LucD
Leadership
Leadership

A Windows editor, like Notepad for example, expects a CR-LF to seperate lines in a text file.

Those are not there in a file produced by Start-Transcript.

Before sending the file as an attachment to an email, you can make sure the CR-LF sequence is there.

For example like this

[string]::Join("`r`n",(Get-Content C:\transcript.txt)) | Out-File C:\transcript.txt


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Biyouk
Contributor
Contributor

Thanks Luc

Need more information if you can. I'm reading in a variable $MyReport the output file which was created by "start/stop -transcript" and send it to email.

$MyReport = (get-content $Filename)
$mailer = new-object Net.mail.SMTPclient($SMTPSRV)
$msg = new-object Net.mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $MyReport)
$msg.IsBodyHTML = $true
$mailer.send($msg)

Could you explain me how integrate your command to convert $Myreport with CR-LF sequence ?

0 Kudos
LucD
Leadership
Leadership

In which application is the attachment opened ?

It depends also on the application how the LF or CR-LF is interpreted.

There is for example a difference between Notepad and Wordpad.

Btw the Send-MailMessage cmdlet is a lot easier to use.

Try something like this

Start-Transcript -Path $Filename 
...
Stop-Transcript

[
string]::Join("`r`n",(Get-Content $Filename)) | Out-File $Filename
Send-MailMessage
-From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -Attachments $Filename


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Biyouk
Contributor
Contributor

LUC

Thanks for your example. This works properly. Attachment is txt file, I use notepad.

If I want to copy the contain of $Filename under the body of the mail, how can I process ?

I tried to use the parameter -body or -bodyAsHtml I got an error message

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.

At D:\support\Scripts\Resource_Pool_amavcs01\Test.ps1:225 char:17

+ Send-MailMessage <<<< -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -BodyAsHtml $MyReport -Attachments $Filename1

+ CategoryInfo : InvalidArgument: (Smiley Happy , ParameterBindingException

+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

$MyReport = (get-content $Filename)

  1. To format properly the contains of Transcript to readable notepad document

::Join("`r`n",(Get-Content $Filename)) | Out-File $Filename

$MyReport = (get-content $Filename)

Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -BodyAsHtml $MyReport -Attachments $Filename1

JEFF

0 Kudos
Biyouk
Contributor
Contributor

LUC

Thanks for your example. This works properly. Attachment is txt file, I  use notepad.

If I want to copy the contain of $Filename under the body of the mail, how can I process ?

I tried to use the parameter -body or -bodyAsHtml I got an error message

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At D:\support\Scripts\Resource_Pool_amavcs01\Test.ps1:225 char:17
+ Send-MailMessage <<<<  -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -BodyAsHtml $MyReport -Attachments $Filename1
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
$MyReport = (get-content $Filename)
# To format properly the contains of Transcript to readable notepad document
[string]::Join("`r`n",(Get-Content $Filename)) | Out-File $Filename
$MyReport = (get-content $Filename)
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -BodyAsHtml $MyReport -Attachments $Filename1

JEFF

0 Kudos
LucD
Leadership
Leadership

You can do something like this

$MyReport = (get-content $Filename) | ConvertTo-Html | Out-String 
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPSRV -BodyAsHtml -Body $MyReport

We take the content of the file and convert it to HTML code.

The HTML conversion normally takes care of the CR-LF sequences.

The Body parameter takes a [string] object, that's why we use the Out-String cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference