Solved

Write-output result on the same line

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

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.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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