?
Solved

Invoke-Expression not keeping lines

Posted on 2013-11-18
3
Medium Priority
?
226 Views
Last Modified: 2013-11-18
I'm trying to get a command to run on one of our servers, and then deal with the output. Problem is that the output is being displayed on one line, and not separating out onto its separate lines:-
$cmd = "openfiles"
$cmdOutput = Invoke-Expression $cmd

write-host $cmdOutput

Open in new window


Is it possible to somehow keep the lines instead of it all going onto one massive line?

Thank you
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39656462
Try by using echo ( Write-output ) instead of write-host

$cmd = "openfiles"
$cmdOutput = Invoke-Expression $cmd

$cmdOutput

Open in new window


$cmd = "openfiles"
$cmdOutput = Invoke-Expression $cmd

Echo $cmdOutput

Open in new window

0
 

Author Comment

by:tonelm54
ID: 39656556
Ok, by Echoing instead of write-host seems to work, however when I include it into my variable, it is still one line:-

$cmd = "openfiles"
$cmdOutput = Invoke-Expression $cmd

Echo $cmdOutput

$msgBody = "Please find below\n\n" + $cmdOutput

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 39656633
To send mail, try this and see if it works for you..
$msgBody = "Please find below <BR> $(($cmdOutput -split "`n")  -join '<BR>')"

Send-MailMessage -BodyAsHtml $msgBody -From from@domain.com -To to@domain.com -SmtpServer smtp.domain.com -Subject "Test"

Open in new window

0

Featured Post

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question