Powershell-删除X天之前的文件

时间:2018-10-26 05:09:06

标签: powershell

我具有以下脚本,该脚本旨在删除X天之前的文件,但它不会执行任何操作。我让它向TXT文件输出一条消息,说明是否成功,但仍然没有成功。有什么想法吗?

$Path = "H:\Test\Test2"
$Days = "-0"
$CurrentDate = Get-Date
$Extension = "*."
$DatetoDelete = $CurrentDate.AddDays($Days)
$DeleteHistoryFile = "H:\Test\TestLogDeleteHistory.txt"
$Files = Get-ChildItem $Path -Include $extension | Where-Object {$_.LastWriteTime -lt $DatetoDelete}

if ($Files -ne $null) 
    {
        foreach ($File in $Files) {
            $Message = "$file has been deleted based on a $Days day rentention policy."
            $Message | Out-File $DeleteHistoryFile -Append
            Remove-Item $File.FullName | out-null
            }             
        }
    else
    {
    $message_no_files = "$DatetoDelete`: There are no files older than $Days days, no files were be deleted"
    $message_no_files | Out-File $DeleteHistoryFile -Append
    }

我的脚本的区别在于,无论脚本是否成功,我都需要输出它。

有人可以解释一下如何将其从本地计算机运行到远程服务器。设置时我需要考虑什么?还是直接在那个盒子上运行会更容易?

0 个答案:

没有答案