Solved

Write-output result on the same line

Posted on 2014-02-12
4
4,594 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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.

730 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