lock on log files

Welcome Forums General PowerShell Q&A lock on log files

This topic contains 2 replies, has 1 voice, and was last updated by

 
Participant
5 years, 1 month ago.

  • Author
    Posts
  • #11411

    Participant
    Points: 0
    Rank: Member

    i am writing to log files like this :

    Function LogWrite
    {
       Param ([string]$str,[bool]$bPrintDate=$true);
       if ($bPrintDate -eq $false) {
        Add-content $global:LogPathName -Encoding Unicode -value $str;
       }
       else {
           	$now=Get-Date;
        	$str=[string]::concat($now,"`t",$str);
           	Add-content $global:LogPathName -Encoding Unicode -value $str;
    	}
    }
    

    and getting error :

    Add-Content : The process cannot access the file 'D:\Logs\AD-AddUserManagers\20131113.log' because it is being used by
    another process.

    i don't see reason for this, i don't have multiple process that write to the same log file?

  • #11412

    Participant
    Points: 0
    Rank: Member

    this happens when i run the script from the second time in the same powershell window.

  • #11414

    Participant
    Points: 0
    Rank: Member

    solved, i had email attachemnt in function that need the dispose method and locked the file

The topic ‘lock on log files’ is closed to new replies.