Solved

Invoke-Expression not keeping lines

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 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