locked
Powershell writing to text file issue RRS feed

  • Question

  • I am writing powershell script to read file names from C drive and write to text file. I am trying to log start time and end time before and after logging data. I see the issue that

    The process cannot access the file 'C:\Logs.txt' because it is being used by another process.

    Is there any option to write to open text files.

    function log($string)
    {
    $logfile = "C:/Logs.txt"
    
       write-host $string 
       $string | out-file -Filepath $logfile -append
    }
    
    
    workflow testflow{
    Param ($folderPath)
     $Files = Get-ChildItem $folderPath  
     log 'starting'
        ForEach -Parallel($file in $Files)
        {
          try{
             log $file.FullName          
           }
         Catch{
           log 'error occured'
         }
        }
     log 'end'
    }

    chandra shekar thota

    Wednesday, September 28, 2016 3:48 AM

Answers

  • That means, that you have to change your function. Don't use a static file name, e.g. use some random numbers:

    function log($string)
    {
    $logfile = "C:/Log_" + ((1..10 | foreach{Get-Random -minimum 0 -maximum 10}) -join "") + ".txt"
    
       write-host $string 
       $string | out-file -Filepath $logfile -append
    }

    or even better, use a time key:

    $logfile = "C:/Log_" + (Get-Date).Ticks + ".txt"

    • Proposed as answer by hpotsirhc Tuesday, October 4, 2016 12:42 PM
    • Marked as answer by Hello_2018 Thursday, October 6, 2016 5:57 AM
    Wednesday, September 28, 2016 7:12 AM

All replies

  • You have to use separate log files for each process in the parallel loop.


    \_(ツ)_/

    Wednesday, September 28, 2016 4:00 AM
  • How can I add different log files. 

    chandra shekar thota

    Wednesday, September 28, 2016 4:27 AM
  • You would have to use a different name for each loop iteration.


    \_(ツ)_/

    Wednesday, September 28, 2016 4:30 AM
  • Will name like below work? Or if parallel processing in more faster then is there any other option in powershell to make a unique name like sessionid in asp.net?

    Logs_$(get-date -format `"yyyyMMdd_hhmmsstt`").txt


    chandra shekar thota

    Wednesday, September 28, 2016 4:48 AM
  • Every iteration of the loop has to use a different file name.  It doesn't matter what the name is as long as you have different names.

    \_(ツ)_/

    Wednesday, September 28, 2016 4:50 AM
  • That means, that you have to change your function. Don't use a static file name, e.g. use some random numbers:

    function log($string)
    {
    $logfile = "C:/Log_" + ((1..10 | foreach{Get-Random -minimum 0 -maximum 10}) -join "") + ".txt"
    
       write-host $string 
       $string | out-file -Filepath $logfile -append
    }

    or even better, use a time key:

    $logfile = "C:/Log_" + (Get-Date).Ticks + ".txt"

    • Proposed as answer by hpotsirhc Tuesday, October 4, 2016 12:42 PM
    • Marked as answer by Hello_2018 Thursday, October 6, 2016 5:57 AM
    Wednesday, September 28, 2016 7:12 AM