none
Can't append strings to output file using System.IO.Streamwriter RRS feed

  • Question

  • OUTPUT TO FILE

    The following, is written to the out file every time the script runs.  The next time it runs, the lines are overwritten, when updated timestamps.  I want the new lines to be appended to the file every time the script runs.

    01/29/2016 22:15:55 Script started.
    01/29/2016 22:15:55 Write this to the logfile1: 
    01/29/2016 22:15:55 Write this to the logfile2: 

    ************************************************************************************

    SCRIPT BEGINS BELOW

    $StartTime = get-date
    $Year = $StartTime.year
    $Month = $StartTime.month
    $Day = $StartTime.day
    $now=Get-Date
    $LogFileName = "C:\PowerShell\Test_Log_" + $Year + "-" + $Month + "-" + $Day + ".log"
    $LogFile = New-Object System.IO.StreamWriter $LogFileName 

    Try
    {
    Function LogEntry($message)
    {
    $now=Get-Date
    $LogFile.append
    $LogFile.WriteLine("$now " + $message)
    $LogFile.Flush()
    }
    LogEntry("Script started.")
    LogEntry("Write this to the logfile1: " + $Path)
    LogEntry("Write this to the logfile2: " + $Path)
    }
    Catch
    {
    }
    Finally
    {
    $LogFile.Close()
    $LogFile.dispose()
    }



    Saturday, January 30, 2016 3:26 AM

Answers

  • This is how we do this in PowerShell

    Function LogEntry{ Param ( $message, $LogFileName = ('Test_Log_{0:MM-dd-yyyy}.log' -f [DateTime]::Today) ) '[{0}]{1}' -f [DateTime]::Now,$message | Out-File $LogFileName -Append } $path=$pwd LogEntry 'Script started.' LogEntry "Write this to the logfile1: $Path" LogEntry "Write this to the logfile2: $Path"

    # list entries
    cat ('Test_Log_{0:MM-dd-yyyy}.log' -f [DateTime]::Today)



    \_(ツ)_/


    • Edited by jrv Saturday, January 30, 2016 5:30 AM
    • Marked as answer by BigRedWeather Sunday, January 31, 2016 2:00 AM
    Saturday, January 30, 2016 5:29 AM
  • You will hate my bill.

    search for  "powershell string format".


    \_(ツ)_/

    • Marked as answer by BigRedWeather Monday, February 1, 2016 4:32 PM
    Sunday, January 31, 2016 8:38 PM

All replies

  • This is how we do this in PowerShell

    Function LogEntry{ Param ( $message, $LogFileName = ('Test_Log_{0:MM-dd-yyyy}.log' -f [DateTime]::Today) ) '[{0}]{1}' -f [DateTime]::Now,$message | Out-File $LogFileName -Append } $path=$pwd LogEntry 'Script started.' LogEntry "Write this to the logfile1: $Path" LogEntry "Write this to the logfile2: $Path"

    # list entries
    cat ('Test_Log_{0:MM-dd-yyyy}.log' -f [DateTime]::Today)



    \_(ツ)_/


    • Edited by jrv Saturday, January 30, 2016 5:30 AM
    • Marked as answer by BigRedWeather Sunday, January 31, 2016 2:00 AM
    Saturday, January 30, 2016 5:29 AM
  • I posted my question at 10PM and had a good answer by 5AM, 7 hours later.  Can't beat that.  

    Thanks,

    BigRedWeather

    Sunday, January 31, 2016 2:00 AM
  • I do have another detailed question about this.

    I can't figure out how to modify the line below to change the output

    '[{0}]{1}' -f [DateTime]::Now,$message | Out-File $LogFileName -Append

    I have looked all over web and I can't find anything that will let me address the formatting of "[DateTime]"  I am sure it is out there, but I can't find it.

    Do you know of a site on the web that explains in detail the Parameters for a function and the properties that effect the DateTime?

    My output now

    [1/30/2016 7:21:39 AM]Write this to the logfile2:

    What I would like

    [2016/1/30 7:21:39 AM]Write this to the logfile2:

    OR

    20160130072139AM Write this to the logfile2:

    That's asking a lot.  Answer what you can.

    BigRedWeather

    Sunday, January 31, 2016 2:11 AM
  • The answer is listed all over the web.

    [{0:yyyy-MM-dd}]{1}' -f [DateTime]::Now,$message | Out-File $LogFileName -Append


    \_(ツ)_/

    Sunday, January 31, 2016 2:44 AM
  • That reminds me of a story about Henry Ford.  His assembly line lived or died on the electricity that powered it.

    One day, the generator quit.  Henry Ford called the technician who installed it.  He came.  Ford observed him "tinker around for a few minutes" and poof, the generator sprang to life.

    For received a bill for "Fixing the generator $10,000"

    When Ford got the bill he scrolled across it "I'm not paying $10,000 for a few minutes of tinkering around" and sent it back to the technician.

    A new bill came again a few days later

    "For a few minutes of tinkering around $10

    For knowing where to tinker around $9990"

    Upon receipt, Henry ford paid the bill, because he knew that his entire assembly line only worked because he had people who knew "Exactly where to tinker around."  

    Knowing where the problem or the answer is all important information.

    Sunday, January 31, 2016 8:21 PM
  • You will hate my bill.

    search for  "powershell string format".


    \_(ツ)_/

    • Marked as answer by BigRedWeather Monday, February 1, 2016 4:32 PM
    Sunday, January 31, 2016 8:38 PM