Solved

Invoke-Expression not keeping lines

Posted on 2013-11-18
3
224 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 500 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

726 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