Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get-Content issue with formatting

426 views
Skip to first unread message

Don Pedro

unread,
Nov 4, 2009, 9:31:01 AM11/4/09
to

I have a script which I run as follows.

Powershell -command "& {C:\MyScript.ps1 | out-file x:\Results.txt}"

The output in the file looks as follows...

=========================================
Stopped Services on Exchange 2007 Servers
=========================================
Server : ServerName
ServicesNotRunning : {MSExchangeSearch, MSFTESQL-Exchange}

=========================================
Stopped Services on Exchange 2003 Servers
=========================================

Server Name State
------ ---- -----
Server1 MSExchangeMGMT Stopped
Server2 MSExchangeMTA Stopped
Server3 MSExchangeMGMT Stopped

All nicely formatted when you open the text file. But it looks like this
when I use Get-Content command.

=========================================Stopped Services on Exchange 2007
Servers=========================================Server :
ServerName ServicesNotRunning : {MSExchangeSearch,
MSFTESQL-Exchange}=========================================
Stopped Services on Exchange 2003
Servers=========================================Server Name
State ------ ---- ----- Server1 MSExchangeMGMT Stopped
Server2 MSExchangeMTA Stopped Server3 MSExchangeMGMT Stopped

So, anyone know how do I keep the formatting in the body of the email?

Thanks in advance.

RichS [MVP]

unread,
Nov 4, 2009, 10:10:01 AM11/4/09
to
It looks like you don't have new line markers on the end of some of your
lines which is causing Get-Content to treat them as single lines.

What does the file look like if you open it in Notepad?

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

Don Pedro

unread,
Nov 4, 2009, 10:40:08 AM11/4/09
to

Hi Richard,

It looks fine in notepad. In fact, the way it displays in Notepad is exactly
how I would like it to appear in the body of the email.

Larry__Weiss

unread,
Nov 4, 2009, 10:42:27 AM11/4/09
to
How are you inserting it into email?

- Larry


Don Pedro wrote:
> It looks fine in notepad. In fact, the way it displays in Notepad is exactly
> how I would like it to appear in the body of the email.
>
> "RichS [MVP]" wrote:
>> It looks like you don't have new line markers on the end of some of your
>> lines which is causing Get-Content to treat them as single lines.
>> What does the file look like if you open it in Notepad?

John Vottero

unread,
Nov 4, 2009, 12:52:34 PM11/4/09
to

Are you sure this isn't just Outlook trying to be helpful? It likes to
remove newline characters from plain text e-mail. Does is say "Extra line
breaks in the message were removed" near the menu bar?

"Don Pedro" <DonP...@discussions.microsoft.com> wrote in message
news:3D737A64-6C57-408D...@microsoft.com...

Don Pedro

unread,
Nov 4, 2009, 1:36:01 PM11/4/09
to
Hi Larry,

I use $msg.Body = Get-Content "x:\MorningChecksResults.txt"

As I said, opening the file in Notepad it displays very well and so I have
just attached it. But I would prefer to have no attachment and simply have
the results appear in the email body as they appear in the text file - nicely
formatted.

Peter

"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Nov 4, 2009, 3:57:59 PM11/4/09
to
Using an idea from
http://www.powershellcommunity.org/Forums/tabid/54/aff/1/aft/3514/afv/topic/afpgj/1/Default.aspx

try

$msg.Body = (Get-Content "x:\MorningChecksResults.txt" | Out-String)

The discussion in that webpage of how Get-Content interacts in a pipeline is
interesting.

Larry__Weiss

unread,
Nov 4, 2009, 5:36:12 PM11/4/09
to
The transcript below gives you an idea what the difference is:

An array of strings is bound to $msg by this code

PS C:> $msg = Get-Content "Results.txt"
PS C:> $msg[0]
=========================================
PS C:> $msg[1]


Stopped Services on Exchange 2007 Servers

PS C:> $msg[2]
=========================================
PS C:> $msg[-2]
Server2 MSExchangeMTA Stopped
PS C:> $msg[-1]
Server3 MSExchangeMGMT Stopped


An array of characters with embedded line delineation
is bound to $msg by using this code

PS C:> $msg = (Get-Content "Results.txt" | Out-String)
PS C:> $msg[0]
=
PS C:> $msg[1]
=
PS C:> $msg[2]
=
PS C:> ("0x{0:x}" -f [int][char] $msg[-2])
0xd
PS C:> ("0x{0:x}" -f [int][char] $msg[-1])
0xa

- Larry

Larry__Weiss wrote:
> Using an idea from
> http://www.powershellcommunity.org/Forums/tabid/54/aff/1/aft/3514/afv/topic/afpgj/1/Default.aspx
>
> try
>
> $msg.Body = (Get-Content "x:\MorningChecksResults.txt" | Out-String)
>
> The discussion in that webpage of how Get-Content interacts in a
> pipeline is interesting.
>
>

> Don Pedro wrote:
>> Hi Larry,
>> I use $msg.Body = Get-Content "x:\MorningChecksResults.txt"
>> As I said, opening the file in Notepad it displays very well and so I
>> have just attached it. But I would prefer to have no attachment and
>> simply have the results appear in the email body as they appear in the
>> text file - nicely formatted.
>> Peter
>>
>> "Larry__Weiss" wrote:
>>> How are you inserting it into email?
>>>

Don Pedro

unread,
Nov 5, 2009, 11:57:01 AM11/5/09
to
Nope, not an Outlook issue.

Larry was spot on.

"John Vottero" wrote:

> .
>

Don Pedro

unread,
Nov 5, 2009, 11:58:04 AM11/5/09
to

Thanks Larry, got that sorted now simply by Outputting each line to the text
file using the `r`n switch.

Nicely formatted now...


"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Nov 5, 2009, 12:03:18 PM11/5/09
to
Can you show us how you coded it?

- Larry

0 new messages