2

警告:我的无知 - 道歉

我想用函数写入日志文件:“Write-Log”,例如

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

(从http://poshcode.org/2566窃取)

我正在使用我通过 . 包括。

我想通过 Write-Log 在我的日志文件中记录哪些文件已存档。

当前存档详细信息是:

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

如何插入我的 Write-Log 来记录我已归档的那些文件?

谢谢

4

1 回答 1

3

对于从 Get-ChildItem 返回的每个 fileinfo 对象,您可以从 ForEach-Object cmdlet 中调用 Move-Item 和 Write-Log。例子:

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
}
于 2013-05-15T14:54:38.480 回答