【问题标题】:Powershell: Record files moved to log file, via write-logPowershell:记录文件移动到日志文件,通过写日志
【发布时间】:2013-05-15 13:57:46
【问题描述】:

警告:我的无知 - 道歉

我想用函数写入日志文件:“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 来记录我已归档的那些文件?

谢谢

【问题讨论】:

    标签: logging powershell archive


    【解决方案1】:

    对于从 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
    }
    

    【讨论】:

    • thnx,为我工作。仅供参考/FWIW 1) $fileInfo.fullname 2) 我确定我看到了类似的答案和随后的帖子,反对管道末端的 foreach - 嗯 - 不介意,它有效并且文件数量我'我正在转变不要认为我正在寻求提高性能的领域......'agin tar。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2019-05-30
    • 2011-02-12
    • 2019-01-11
    相关资源
    最近更新 更多