【问题标题】:Powershell archive scriptPowershell 归档脚本
【发布时间】:2012-08-23 15:49:11
【问题描述】:

我刚开始使用 PowerShell,在书本和大量 Google 搜索的帮助下,我想出了这个脚本。我遇到的问题是它没有保留文件夹结构。它正在检查一个网络位置是否存在超过 5 天的文件。如果超过 5 天,它会将其移动到不同的网络位置。在第二个位置,它会检查超过 30 天的文件并删除这些文件。

$movepath = "C:\test"
$archpath = "C:\test2"
$deletepath = "C:\files"
$movedays = "5"
$deletedays = "30"
$datetime = get-date
$deletelog = "c:\logs\deletelog.txt"
$movelog = "c:\logs\movelog.txt"
$errorlog = "c:\logs\errorlog.txt"

write-progress -activity "Archiving Data" -status "Progress:"

Get-Childitem -Path $movepath -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$movedays)} |
 ForEach {
$filename = $_.fullname
try
{
Move-Item $_.FullName -destination $archpath -force -ErrorAction:SilentlyContinue
"Moved $filename to $archpath at $datetime successfully" | add-content $movelog
}
catch
{
"Error moving $filename: $_ " | add-content $errorlog
}
}
Get-Childitem -Path $deletepath | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$deletedays)} |
ForEach {
$filename = $_.fullname
try
{
Remove-Item $_.FullName -force -ErrorAction:SilentlyContinue
"Removed $filename at $datetime successfully" | add-content $deletelog
}
catch
{
 "Error moving $filename: $_ " | add-content $errorlog
}
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您需要添加逻辑来维护文件夹结构。

    $FileName = $_.FullName.Replace($MovePath, $ArchPath);
    

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 2022-01-22
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2020-04-12
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多