lock on log files

This topic contains 2 replies, has 1 voice, and was last updated by Profile photo of wantto wantto wantto wantto 3 years, 7 months ago.

  • Author
    Posts
  • #11411
    Profile photo of wantto wantto
    wantto wantto
    Participant

    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
    Profile photo of wantto wantto
    wantto wantto
    Participant

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

  • #11414
    Profile photo of wantto wantto
    wantto wantto
    Participant

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

You must be logged in to reply to this topic.