2017-01-06 2 views
3

Powershellはかなり新しくなっていますが、定義済みのCreated Dateより古いファイルや特定のファイルタイプを除外するスクリプトをまとめています。しかし、私は冗長とファイルロギング出力の両方を組み込むのに苦労しています。私はオンラインで見つかった様々な方法を試してきましたが、Out-Fileが最も適切だと思いますが、私は単にそれを動作させることはできません。誰かが助けてくれると願っています!Powershell(PS2)logging Remove-Item

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 
+3

'Tee- Object'コマンドレットは、出力を2方向にリダイレクトします。出力をファイルまたは変数に格納し、パイプラインに送ります。 – JosefZ

答えて

4

ログファイルには何もコンテンツが送信されていません。

のコメントを外し$logfile宣言し、インスタンスのためにこれを使用します。あなたがそれらを削除する前に

ForEach-Object { 
    Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf 
    "Removed $($_.FullName)" | Out-File -FilePath $logfile -Append 
} 
4

あなたがやりたいことはただTee-Objectファイルです。同様に、文字通りだけTee-ObjectであなたのOut-Fileを置き換える:ログに出力さそう、コンソールにあなたの出力の場合と同じようにフォーマットされます

  1. 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)) 
    

    を唯一の問題は、ということです

  2. 削除するかどうかにかかわらずログは同じになります(つまり、-Whatifは削除されませんが、ログは停止しません)
+1

あなたの解決策はもっと適切です:)(あなたの解決策、私は意味します) – sodawillow

+0

編集のおかげで、それはより明確になります。 – Jaykul

+0

助けてくれてありがとう!だから、私はTee-Objectが-Appendスイッチが気に入らないので動かすことができませんでした。私はあなたの指針を使用しましたが、別のコマンドを使用することにより、もう少し読んで作業しました(非常に上品ではありませんが)。私のコーディングは目が覚めて悪いです(残念ながら私はまだ学んでいます)が、私は以下の答えとして、私が何をしたのかを実証しました。 –

1

はここ

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)) 
0

Aは前にしながら、私が作成している...作業ログを取得するために私の更新されたコードですあなたのようなインラインロギングを行うことができますLog-Entry framework

Remove-Item -Path (Log "Removing:" $_.FullName ?) -Force -WhatIf:$WhatIf