Solved

Write-output result on the same line

Posted on 2014-02-12
4
4,418 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
  • 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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

840 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