?
Solved

Write-output result on the same line

Posted on 2014-02-12
4
Medium Priority
?
5,268 Views
1 Endorsement
Last Modified: 2014-02-12
Is there an easy way to append the result ($fullvsspath (gci$fullvsspath-R|Measure-Object).count in the same line? It is a script to count number of files on a directory.

$vsspath="c$\temp\abc";
gc d:\temp\servers1.txt|sort|%{
$fullvsspath="\\$_\$vsspath"
if(!(test-path$fullvsspath))
    {
    write-output "$fullvsspath unreachable"|Out-File -Encoding ASCII -a servers1_vsscount.txt;
    }
else
    {
    write-output $fullvsspath (gci$fullvsspath-R|Measure-Object).count|Out-File -Encoding ASCII -a -FilePath servers1_vsscount.txt;
    }
}

Open in new window


I have no issue with write-host to display on screen but I have problem writing them on a same line. Currently it is writing in different line, for example -

serverA
123
serverB
123

Open in new window

1
Comment
Question by:Mazdajai
[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
  • 2
4 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39855050
You just have to construct your string to include all the information you want.  Something like the below should work.
write-output "$fullvsspath $((gci$fullvsspath-R|Measure-Object).count)"|Out-File -Encoding ASCII -a -FilePath servers1_vsscount.txt;

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39855095
Thanks, unbelievable simple.

I tried double quote but without the dollar sign / bracket ... I got the below instead.

Isn't double quote will treat them as variables? Why $() is needed?

C:\temp (gci C:\temp|Measure-Object).count

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 39855114
If you just had
write-output "$fullvsspath $someothervar"
Then yes, each variable would be evaluated.
But when you have something like
write-output "$fullvsspath $someothervar.property"
$someothervar is still evaluated in its entirety (instead of just retrieving the property you're interested in), and then ".property" is appended as part of the string.  You need to tell PS to evaluate the result of $someothervar.property and then substitute the returned value into the string.  This is done by using the subexpression notation $().

In your post above, the variables are still evaluated, but there's nothing to tell it that "(gci C:\temp|Measure-Object).count" should be evaluated for its result.  That's what the subexpression notation does.
0
 
LVL 21

Author Closing Comment

by:Mazdajai
ID: 39855124
Thank you for the explanation!
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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