none
Redirect (Save) Output to an Existing File RRS feed

  • Question

  • Hello Everyone,

    Just to start with, I am a new user of Powershell so please excuse me if you find my inquiry dumb. :)

    I have a command which is "cleartool space -vob \test | select -last 1 | ForEach-Object {Write-Host ($_ -split ' ')[5,7,8]}" that gives me an output of \test 400 Mb. I want this output to be saved in a file called D:\BackupFiles\result.txt.

    I tried executing the command "cleartool space -vob \test | select -last 1 | ForEach-Object {Write-Host ($_ -split ' ')[5,7,8]} | Out-File D:\BackupFiles\result.txt -Append" but this doesn't save the output to the file. Instead, it shows only the output in the screen.

    I tried also "cleartool space -vob \test | select -last 1 |  ForEach {Write-Output ($_ -split ' ')[5,7,8]} | Out-File D:\BackupFiles\result.txt -Append" instead of the Write-Host and it indeed saved to the file but instead of a one-liner, it saves as 3 lines like this:

    \test

    400

    Mb

    Can you please tell me what is wrong with the command I executed and what is the right way instead?

    Your help is much appreciated.

    Best regards,

    Maria


    • Edited by MariaCarmela Friday, October 18, 2019 11:08 AM wrong name quoted
    Friday, October 18, 2019 11:06 AM

Answers

  • First of all - please format your code as code using the code posting tool: How to Use the Code Feature in a TechNet Forum Post.

    Thanks.

    With the -split operator you split a string into an array of smaller strings. Obviously you actually don't want this ... so so you have to re-join these strings back together   ;-)

    cleartool space -vob \test | 
        Select-Object -last 1 | 
            ForEach-Object { (($_ -split ' ')[5, 7, 8] ) -join ' ' } | 
                Out-File D:\BackupFiles\result.txt -Append
    It would have been helpful to see at least one line of the original output of your "cleartool"


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrvModerator Friday, October 18, 2019 5:28 PM
    • Marked as answer by MariaCarmela Tuesday, October 22, 2019 8:16 AM
    Friday, October 18, 2019 11:19 AM

All replies

  • First of all - please format your code as code using the code posting tool: How to Use the Code Feature in a TechNet Forum Post.

    Thanks.

    With the -split operator you split a string into an array of smaller strings. Obviously you actually don't want this ... so so you have to re-join these strings back together   ;-)

    cleartool space -vob \test | 
        Select-Object -last 1 | 
            ForEach-Object { (($_ -split ' ')[5, 7, 8] ) -join ' ' } | 
                Out-File D:\BackupFiles\result.txt -Append
    It would have been helpful to see at least one line of the original output of your "cleartool"


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrvModerator Friday, October 18, 2019 5:28 PM
    • Marked as answer by MariaCarmela Tuesday, October 22, 2019 8:16 AM
    Friday, October 18, 2019 11:19 AM
  • Hello,

    Thank you very much! This really solved my problem.

    The output of the cleartool command is:

    Total usage 2015-10-18T05:35:40+02:00 for vob \test is 400 Mb

    Best regards,

    Maria

    Friday, October 18, 2019 11:27 AM