Powershell 脚本删除特定日期之后的旧备份

问题描述 投票:0回答:1

我正在尝试编写一个 powershell 脚本来删除特定日期之后的旧备份。我的文件夹将包含每周左右写入的备份列表。 我的逻辑是首先找到早于某个日期的所有备份,然后删除它们。所以我需要提取日期和相应的备份。我尝试在 foreach 语句中提取这些内容,但语法似乎有问题。关于如何进行此操作有什么建议吗?

我的 powershell 脚本如下,

## Path
$path = "C:\SFTP_Root\SMGR"

## Get the current date
$Cur_dte = Get-Date
echo "Today is $Cur_dte"

## Old date (Subtract current date by 2 months i.e. 60 days)
$Old_dte = (Get-Date).AddDays(-60)
echo "Old date is $Old_dte"

## Loop to extract the date and name of the backup)
foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty LastWriteTime) -and ($file in Get-ChildItem -Path $path | select -ExpandProperty Name))
    {
        if ($i -lt $Old_dte)
        {
        echo "$file is old; will be deleted"
        }
    elseif ($i -gt $Old_dte)
        {
        echo "$file is recent; won't be deleted"
        }
    }
    

而且foreach语句有很多语法错误,

t C:\Temp\backups_clean.ps1:14 char:10
+ foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ...
+          ~
Missing variable name after foreach.
At C:\Temp\backups_clean.ps1:14 char:14
+ foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ...
+              ~~
Unexpected token 'in' in expression or statement.
At C:\Temp\backups_clean.ps1:14 char:13
+ foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ...
+             ~
Missing closing ')' in expression.
At C:\Temp\backups_clean.ps1:14 char:81
+ ... ChildItem -Path $path | select -ExpandProperty LastWriteTime) -and ($ ...
+                                                                 ~
Unexpected token ')' in expression or statement.
At C:\Temp\backups_clean.ps1:14 char:95
+ ...  $path | select -ExpandProperty LastWriteTime) -and ($file in Get-Chi ...
+                                                                ~~
Unexpected token 'in' in expression or statement.
At C:\Temp\backups_clean.ps1:14 char:94
+ ... ath $path | select -ExpandProperty LastWriteTime) -and ($file in Get- ...
+                                                                  ~
Missing closing ')' in expression.
At C:\Temp\backups_clean.ps1:14 char:153
+ ... d ($file in Get-ChildItem -Path $path | select -ExpandProperty Name))
+                                                                        ~
Unexpected token ')' in expression or statement.
At C:\Temp\backups_clean.ps1:14 char:154
+ ... d ($file in Get-ChildItem -Path $path | select -ExpandProperty Name))
+                                                                         ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingVariableNameAfterForeach
powershell
1个回答
0
投票

不能像这样在 for 循环中使用两个变量。相反,迭代所有文件并在循环内访问它们的属性:

foreach ($file in Get-ChildItem -Path $path) {
    if ($file.LastWriteTime -lt $Old_dte) {
        echo "$file is old; will be deleted"
    }
    else {
        echo "$file is recent; won't be deleted"
    }
}

作为旁注:我会避免使用

-lt
-gt
这两个条件。如果日期完全相同怎么办?如果您仅使用一种条件,那么这些文件会发生什么就很清楚了。

© www.soinside.com 2019 - 2024. All rights reserved.