none
I need a script to write to a file and not prevent it from being deleted after the script is done RRS feed

  • Question

  • I need a script to write to a file and not prevent it from being deleted after the script is done.  MAYBE IT IS ISE THAT IS PREVENTING THE FILE FROM BEING DELETED WHEN THE SCRIPT IS FINISHED, AS I SAY IN THE COMMENTS ABOVE THE SCRIPT.  BUT NOW, I CAN'T RUN THE SCRIPT WITHOUT AN EXCEPTION.

    My primary Question is: "Script to write to a file and not prevent it from being deleted after the script is done"

    My secondary Question is: This script below, was working properly last night, except for the "Primary Question"  Now, it throws an exception on the Out-File cmdlet.  How can I get around this exception?

    I hate to take up your guys time on what I think are pretty fundamental questions.  I have spent hours in the get-help and online studying the cmdlets and trying different scenarios.  I have tried to make sure I kept the working directory and the whole environment consistent from last night to tonight.

    Is there a gotchas document on the web, something that would help me avoid some pitfalls?  I read up on "'[{0}]{1}' -f $TimeStamp,$message | Out-File $LogFileName -Append" last night and learned alot, but that is where the exception is being thrown now.

    PowershellISESteroids on .Net 4.6

    Win 7 Pro 64

    <#
    To run LoggerFunction.ps1
    -Start Powershell, not PowerShellISE because it keeps a file open until ISE is closed.
    -Place LoggerFunction.ps1 into a directory of your choice that does not have a subdirectory named \LogFiles
    -In Powersheel, make that directory your working directory.
    -Run the script
    -The output file will be found in <Your Directory of Choice>\LogFiles
    #>

    $StartTime = get-date
    $Year = $StartTime.year
    $Month = $StartTime.month
    $Day = $StartTime.day

    $LogFileDir = join-path -path (get-location) -childpath LogFiles
    $LogFileName = $LogFileDir + '\Log_' + $Year + '-' + $Month + '-' + $Day + '.log'
    write-host('LogFileName: ' + $LogFileName)

    Try
        {
        Function Submit-LogEntry
       {
       Param 
       (
       $message
       )

            #Modify the standard u formatted date, for efficiency.  
            #$TimeStamp contains the timestamp written to at the beginninf of each line inside the log file.
            $TimeStamp = get-date -f u
            $TimeStamp = $TimeStamp -replace 'Z',''
            $TimeStamp = $TimeStamp -replace ' ','_'

            #Write a line to the LogFile
            #LINE BELOW IS WHERE THE EXCEPTIONS OCCURS
            '[{0}]{1}' -f $TimeStamp,$message | Out-File $LogFileName -Append
            #'[{0}]{1}' -f $TimeStamp,$message >> $LogFileName   A VARIETY OF OTHER LINES DID NOT HELP
                
       } #Function Submit-LogEntry
        
        write-host (' ')
        write-host('Please allow the scrip to run until you see SCRIPT DONE on the screen.')
        
        Submit-LogEntry('First entry written to the log file.')

        Start-Sleep 2
        Submit-LogEntry('This entry should be 2 seconds after the first entry.')
        
        Start-Sleep 3
        Submit-LogEntry('This entry should be 3 seconds after the second entry.')
        
        write-host (' ')
        write-host ('SCRIPT DONE')

        function Stop-Exit{exit}
        Stop-Exit

        }
    Catch
        {
        }
    Finally
        {
    Exit
        }


    Saturday, February 13, 2016 3:25 PM

Answers

  • I got this function to work.

    All of the lines below work.  Some of them did not work before. 

          #WRITES TO THE LOG FILE AND CAN THE DELETE THE LOGFILE
            #'[{0}]{1}' -f $TimeStamp,$message >> $LogFileName
            #'[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath $LogFileName -Append
            #            PREVIOUSLY THE LINE ABOVE WOULD NOT WORK, NOW IT DOES SO SOMETHING ELSE IN
            #            SCRIPT MUST HAVE CHANGED.
            #'[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath 'C:\powershell\wwtt-prd\LogFiles\Log_2016-2-13.log' -Append
            #     Line above worked only when it had ' ' around the path.
            #     These are Examples from get-help out-file and none of them have ' ' around the path.
            #     To do PowerShell it seems you have to know a lot more that an example in the help, 
            #     and you have to hold your mouth right.  I've learned other languages and they had a
            #     steep learning curve too.  I will learn this one.
            #               PS C:\>get-process | out-file -filepath C:\Test1\process.txt
            #               PS C:\>get-process | out-file C:\Test1\process.txt -noclobber
            #               PS C:\>out-file -filepath C:\Test1\process.txt -inputobject $a -encoding ASCII -width 50
            #---------------------------------------------------

            '[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath $LogFileName -Append

            
    • Marked as answer by BigRedWeather Saturday, February 13, 2016 10:25 PM
    Saturday, February 13, 2016 10:25 PM

All replies

  • The function works properly with this instead of the full line:

    '[{0}]{1}' -f $TimeStamp,$message

    So that tells me my problem is either in the other half of the line, or in how the 2 work together

            | Out-File $LogFileName -Append

    This still throws an exception:

    '[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath ($LogFileName) -Append

    $timestamp
    2016-02-13_14:26:06

    $message
    First entry written to the log file.

    [DBG]: PS C:\Users\weather>> $logfilename
    C:\Users\weather\LogFiles\Log_2016-2-13.log

    When I run the scipt or debug it I get an exception and the LogFiles directory is not created.

    I have looked all over Out-File and find no clues.

    I'm sure the answer is out there and is simple, but I can't find it.

    Saturday, February 13, 2016 7:35 PM
  • I got this function to work.

    All of the lines below work.  Some of them did not work before. 

          #WRITES TO THE LOG FILE AND CAN THE DELETE THE LOGFILE
            #'[{0}]{1}' -f $TimeStamp,$message >> $LogFileName
            #'[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath $LogFileName -Append
            #            PREVIOUSLY THE LINE ABOVE WOULD NOT WORK, NOW IT DOES SO SOMETHING ELSE IN
            #            SCRIPT MUST HAVE CHANGED.
            #'[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath 'C:\powershell\wwtt-prd\LogFiles\Log_2016-2-13.log' -Append
            #     Line above worked only when it had ' ' around the path.
            #     These are Examples from get-help out-file and none of them have ' ' around the path.
            #     To do PowerShell it seems you have to know a lot more that an example in the help, 
            #     and you have to hold your mouth right.  I've learned other languages and they had a
            #     steep learning curve too.  I will learn this one.
            #               PS C:\>get-process | out-file -filepath C:\Test1\process.txt
            #               PS C:\>get-process | out-file C:\Test1\process.txt -noclobber
            #               PS C:\>out-file -filepath C:\Test1\process.txt -inputobject $a -encoding ASCII -width 50
            #---------------------------------------------------

            '[{0}]{1}' -f $TimeStamp,$message | Out-File -filepath $LogFileName -Append

            
    • Marked as answer by BigRedWeather Saturday, February 13, 2016 10:25 PM
    Saturday, February 13, 2016 10:25 PM