?
Solved

Invoke-Expression not keeping lines

Posted on 2013-11-18
3
Medium Priority
?
233 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
  • 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

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
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 anti-spam), the admin…

850 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