none
Solved: Logging : add-content : The process cannot access the file - workaround for this? RRS feed

  • Question

  • How do get around this...seem the file get locked under the process

    The Error I get is:

    Add-content : The process cannot acces the file 'Z:\testPC.log' because it being used by another process.

    the script.

    $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
    $Logfile = "$ScriptDir\$(gc env:computername).log"
    
    Function LogWrite
    {
       Param ([string]$logstring)
       Add-content $Logfile -value $logstring
       write-host $_.FullName
    }
    
    Get-ChildItem -Path "c:\windows\temp" | ForEach-Object {LogWrite $_.FullName;Remove-Item $_.FullName -Recurse}
    


    #LnQ¯\_(ツ)_/¯


    • Edited by LongQuo Friday, May 20, 2016 8:49 AM Solved
    Friday, May 20, 2016 8:20 AM

Answers

  • This:

     Add-content $Logfile -value $logstring

    Should be this:
    $logstring | Out-File $logfile -Append 


    \_(ツ)_/


    • Marked as answer by LongQuo Friday, May 20, 2016 8:48 AM
    • Edited by jrv Friday, May 20, 2016 8:52 AM
    Friday, May 20, 2016 8:32 AM

All replies

  • Not a file name:

    $Logfile = "$ScriptDir\$(gc env:computername).log"

    $Logfile = "$ScriptDir\$env:computername.log"


    \_(ツ)_/

    Friday, May 20, 2016 8:29 AM
  • This:

     Add-content $Logfile -value $logstring

    Should be this:
    $logstring | Out-File $logfile -Append 


    \_(ツ)_/


    • Marked as answer by LongQuo Friday, May 20, 2016 8:48 AM
    • Edited by jrv Friday, May 20, 2016 8:52 AM
    Friday, May 20, 2016 8:32 AM
  • Not a file name:

    $Logfile = "$ScriptDir\$(gc env:computername).log"

    $Logfile = "$ScriptDir\$env:computername.log"

    if i change that i get this 

    add-content: the process cannot access the file 'Z:\.log"


    #LnQ¯\_(ツ)_/¯


    • Edited by LongQuo Friday, May 20, 2016 8:55 AM
    Friday, May 20, 2016 8:44 AM
  • that worked

    the working script 

    $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
    $Logfile = "$ScriptDir\$(gc env:computername)_deleted.log"
    
    Function LogWrite
    {
       Param ([string]$logstring)
       $logstring | Out-File $logfile -Append 
       write-host $_.FullName
    }
    
    Get-ChildItem -Path "c:\windows\temp" | ForEach-Object {LogWrite $_.FullName;Remove-Item $_.FullName -Recurse} 
    
     
    


    #LnQ¯\_(ツ)_/¯

    Friday, May 20, 2016 8:48 AM
  • Do not use Add-Content.

    THIs works on all version of PowerShell

    $Logfile = "$ScriptDir\$env:computername.log"

    $ScriptDir will only have a value if you are running a script file.


    \_(ツ)_/

    Friday, May 20, 2016 8:52 AM
  • This way works even better:

    $ScriptDir = Split-Path $MyInvocation.MyCommand.Path
    $Logfile = "$ScriptDir\$env:computername.log"
    $Logfile
    
    Function AuditFiles{
    	Param(
    		[Parameter(ValueFromPipeline)]$file
    	)
    	Process{
       		$file.Fullname | Out-File $logfile -Append 
       		write-host $file.Fullname -fore green
    		$file
    	}
    }
    
    
    dir | AuditFiles | Remove-Item -WhatIf


    \_(ツ)_/


    • Edited by jrv Friday, May 20, 2016 9:02 AM
    Friday, May 20, 2016 9:01 AM
  • I use this script to create som temp file to test on.

    1..100 | % { New-Item -Path c:\windows\temp -Name “$_.txt” -Value (Get-Date).toString() -ItemType file}
    New-Item -Path c:\windows\temp\test -itemtype directory
    1..100 | % { New-Item -Path c:\windows\temp\test -Name “$_.txt” -Value (Get-Date).toString() -ItemType file}

    it create some files under temp and a subfolder test under temp.

    When i run your script i get everything logged except the content of the subfolder test?

    I added -recurse at the Remove-Item


    #LnQ¯\_(ツ)_/¯

    Friday, May 20, 2016 9:24 AM
  • dir -recurse | AuditFiles | Remove-Item -WhatIf

    \_(ツ)_/

    Friday, May 20, 2016 9:28 AM
  • doesnt work

    Security warning
    Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your
    computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning
    message. Do you want to run Z:\erase.ps1?
    [D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"): r
    Z:\\WIN-37Q5G2I8Q6R_deleted.log
    C:\windows\temp\test
    Confirm
    The item at Microsoft.PowerShell.Core\FileSystem::C:\windows\temp\test has children and the Recurse parameter was not
    specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
    

    dir "c:\windows\temp" -recurse | AuditFiles | Remove-Item 


    #LnQ¯\_(ツ)_/¯

    Friday, May 20, 2016 9:54 AM
  • The original question was solved a long time ago.  Why are you downloading files from the Internet.  They cannot be run without unblocking.

    I posted no files.


    \_(ツ)_/

    Friday, May 20, 2016 10:19 AM
  • I run it from a share inside a vm...

    Thanks for your solution i will work on that...


    #LnQ¯\_(ツ)_/¯


    • Edited by LongQuo Friday, May 20, 2016 10:53 AM
    Friday, May 20, 2016 10:53 AM