Solved

Write-output result on the same line

Posted on 2014-02-12
4
3,855 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 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
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 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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
Comment Utility
Thank you for the explanation!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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 the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

744 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

18 Experts available now in Live!

Get 1:1 Help Now