Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDO Msg - Read txt created by PowerShell - Garbled 1

Status
Not open for further replies.

chipk

IS-IT--Management
Mar 23, 2006
1,226
US
Hopefully that subject makes sense...

I have a variety of scripts that do stuff, output to text file, and then use vbscript to send it to me in an e-mail. I've been working on getting some of these converted over to PowerShell, and everything works fine, except when I try to e-mail it. When the e-mail arrives in my mailbox, it is usually just a couple wing-ding characters, like it's not reading the text file properly or there are some hidden chars. Here's my vbscript code:

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\freespace.txt", ForReading)
BodyText = f.ReadAll
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "me@mail.com"
objEmail.To = "me@mail.com"
objEmail.Subject = "Server Disk Space"
objEmail.TextBody = BodyText
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "smtprelay"
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

If I open the output file from PowerShell in notepad, I can read it fine, Wordpad as well. If nobody's seen anything like this before, I'll assume it's a problem with PowerShell and pursue this from that angle.
 
Ok, so it is indeed something with the formatting of the output from PowerShell. I confirmed this by adding a line to my batch file before sending the text with the vbscript:

c:\type freespace.txt >> freespace2.txt
cscript mailme.vbs

That got rid of whatever illegal characters or formatting that was output by PS.
 
You may wish to ask any future VBScript questions in the VBScript forum (forum329)

This is for VB5/6 questions specifically

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Powershell is outputting in Unicode. Notepad and Wordpad can both spot this and so display the file correctly.

CDOSYS needs to be told that it is working with Unicode. So insert something like the following:

objEmail.Bodypart.Charset = "utf-7"

before this line:

objEmail.TextBody = BodyText
 
Thank you both. I didn't even catch that this was the Visual Basic forum vs. VBScript. Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top