首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell (PS2)日志记录删除项

Powershell (PS2)日志记录删除项
EN

Stack Overflow用户
提问于 2017-01-06 18:35:00
回答 3查看 1.8K关注 0票数 3

我对Powershell相当陌生,但到目前为止,我还在一起编写一个脚本,该脚本删除比定义的创建日期更早的文件,并排除某些文件类型。但是,我很难同时集成详细的和文件日志记录输出。我尝试过各种方法,我在网上发现,我认为-文件是最合适的,但我根本无法使它工作。希望有人能帮忙!

代码语言:javascript
复制
    Set-StrictMode -Version Latest
    # $Logfile = "C:\Temp\Log.log"

    function Remove-Files([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [switch] $WhatIf)

    {
        Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime -and ($_.Name -notlike "*.txt"-and $_.Name -notlike "*.log")} |
        # Out-File -filepath $logfile -append 
        ForEach-Object { Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf}
    }

    Remove-Files -Path "C:\Temp" -DateTime ((Get-Date).AddDays(-10)) # -whatif
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-06 18:38:06

您不会向日志文件发送任何内容。

取消对$logfile声明的注释,例如使用以下内容:

代码语言:javascript
复制
ForEach-Object {
    Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf
    "Removed $($_.FullName)" | Out-File -FilePath $logfile -Append 
}
票数 4
EN

Stack Overflow用户

发布于 2017-01-06 20:10:22

您想要做的只是在删除文件之前对其进行Tee-Object处理。就像,把你的Out-File换成Tee-Object

代码语言:javascript
复制
function Remove-Files {
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [ValidateScript({Test-Path $_})]
        [string]$Path,

        [parameter(Mandatory=$true)]
        [DateTime]$DateTime,

        # Personally, I would pass the log file as a parameter
        # [string]$LogFile,

        [switch]$WhatIf
    )

    Get-ChildItem -Path $Path -Recurse -Force | 
        Where-Object { 
            !$_.PSIsContainer -and 
            $_.CreationTime -lt $DateTime -and 
            ($_.lName -notlike "*.txt" -and $_.Name -notlike "*.log")
        } |
        Tee-Object -Filepath $LogFile -Append |
        Remove-Item -Force -WhatIf:$WhatIf
}


Remove-Files -Path "C:\Temp" -Log "C:\Temp\Rm.log" -DateTime ((Get-Date).AddDays(-10))

唯一的问题是:

  1. 日志中的输出将被格式化为与输出到控制台时相同的格式,因此它与通常记录的不完全相同.
  2. 无论删除与否,日志都是相同的(即:-Whatif使其不删除,但不停止日志)
票数 4
EN

Stack Overflow用户

发布于 2017-01-08 22:59:10

这是我更新的代码,可以让日志正常工作。

代码语言:javascript
复制
    function Remove-FilesCreatedBeforeDate([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [string]$LogFile = "C:\Temp\Log.log", [switch] $WhatIf)
    {
        "LOG START $(Get-Date –f "yyyy-MM-dd HH:mm:ss")" | Out-File -FilePath $logfile -Append 
        Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime -and ($_.Name -notlike "*.txt"-and $_.Name -notlike "*.log")} | 
        ForEach-Object { Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf 
        "$(Get-Date –f o) Removed $($_.FullName)" | Out-File -FilePath $logfile -Append | Write-host "Removed $($_.FullName)"}
        "LOG END $(Get-Date –f "yyyy-MM-dd HH:mm:ss")" | Out-File -FilePath $logfile -Append 
    }

    Remove-FilesCreatedBeforeDate -Path "C:\Temp" -DateTime ((Get-Date).AddDays(-0))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41512084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档