2

Caveat: My Ignorance - apologies

I would like to write to a log file with a function: "Write-Log" e.g.

Write-Log -Message "moved file abcd.efd" -Path C:\MyLog.log

(Stolen from http://poshcode.org/2566)

I am successfully log writing, from an archiving script, with the Write-Log function, which I have added via a . include.

I would like to note to my log file, via Write-Log, which files have been archived.

Current archive details are:

Get-ChildItem -Path $SourceDir |
Where-Object {$_.LastWriteTime.Date -lt (get-date).AddDays(-$Age) -and -not $_.PSIsContainer} |
Move-Item -Destination $MoveDir

How may I plug in my Write-Log to record those files I have archived?

Thanks

1 Answer 1

3

You can call Move-Item and Write-Log from within the ForEach-Object cmdlet for each fileinfo object returned from Get-ChildItem. Example:

Get-ChildItem -Path $SourceDir | `
Where-Object {$_.LastWriteTime.Date -lt (get-date).AddDays(-$Age) -and -not $_.PSIsContainer} | `
ForEach-Object {
$fileInfo = $_
Move-Item -Path $fileInfo -Destination $MoveDir -WhatIf
# Log your message
Write-Log -Message "moved file $fileInfo" -Path C:\MyLog.log
}
Sign up to request clarification or add additional context in comments.

1 Comment

thnx, works for me. FYI/FWIW 1) $fileInfo.fullname 2) I'm sure I saw a similar answer and a subsequent post objected to the foreach on the end of a pipeline - meh - ne'er mind, it works and number of files i'm shifting don't think i'm in the realm of looking to increase performance... 'agin tar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.