Solved

Invoke-Expression not keeping lines

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now