1

我有许多存储在网络共享上的客户备份链,我想创建一个 powershell 脚本来检查备份文件夹是否在过去 4 天内被修改。这样我就可以自动化检查备份是否运行的过程。

文件夹结构如下:\\server\backups\customer1\chain1

该脚本将需要检查每个“链”文件夹的最后修改日期,并输出文件夹路径列表以及声明,以便我可以识别文件夹是否在过去 4 天内更新。

例如: \\server\backups\customer1\chain1 Backups out of date\\server\backups\customer1\chain1 Backups working fine

这是我到目前为止所得到的,但无论修改日期是否早于 4 天,所有文件夹的输出都是相同的。

$backuppaths = get-childitem -path "\\server\backups\customer1\" -Directory | Select-Object FullName

foreach ($path in $backuppaths)
{
  if ($path | Where{$path.LastWriteTime -ge (get-date).AddDays(-$4)})
  {
       "$path Backups out of date"  
  }
  else
  {
       "$path Backups working fine"
  } 
}

作为参考,我使用了相同的脚本并在我的音乐库上运行它,这是输出:

@{FullName=\\db-server\music\2Pac} Backups working fine
@{FullName=\\db-server\music\50 Cent} Backups working fine
@{FullName=\\db-server\music\Adele} Backups working fine
@{FullName=\\db-server\music\Akon} Backups working fine
@{FullName=\\db-server\music\Arctic Monkeys} Backups working fine
@{FullName=\\db-server\music\Avicii} Backups working fine
@{FullName=\\db-server\music\Barry White} Backups working fine
@{FullName=\\db-server\music\Billy Idol} Backups working fine
@{FullName=\\db-server\music\Black Eyed Peas} Backups working fine
@{FullName=\\db-server\music\Bob Marley} Backups working fine
@{FullName=\\db-server\music\Bob Sinclar} Backups working fine
@{FullName=\\db-server\music\Bon Jovi} Backups working fine
@{FullName=\\db-server\music\Bruno Mars} Backups working fine
@{FullName=\\db-server\music\Calvin Harris} Backups working fine
@{FullName=\\db-server\music\Chase and Status} Backups working fine
@{FullName=\\db-server\music\Cher} Backups working fine
@{FullName=\\db-server\music\Coldplay} Backups working fine
@{FullName=\\db-server\music\Cypress Hill} Backups working fine
@{FullName=\\db-server\music\Daft Punk} Backups working fine
@{FullName=\\db-server\music\Disclosure} Backups working fine
@{FullName=\\db-server\music\Dizzee Rascal} Backups working fine
@{FullName=\\db-server\music\DJ Fresh} Backups working fine
@{FullName=\\db-server\music\Dr. Dre} Backups working fine
@{FullName=\\db-server\music\Drake} Backups working fine
@{FullName=\\db-server\music\Duran Duran} Backups working fine
@{FullName=\\db-server\music\Ed Sheeran} Backups working fine
@{FullName=\\db-server\music\Elvis Presley} Backups working fine
@{FullName=\\db-server\music\Eminem} Backups working fine
@{FullName=\\db-server\music\Example} Backups working fine
@{FullName=\\db-server\music\Fleetwood Mac} Backups working fine
@{FullName=\\db-server\music\Frank Sinatra} Backups working fine
@{FullName=\\db-server\music\Future} Backups working fine
@{FullName=\\db-server\music\George Michael} Backups working fine
@{FullName=\\db-server\music\Gorillaz} Backups working fine
@{FullName=\\db-server\music\Green Day} Backups working fine
@{FullName=\\db-server\music\Guns N' Roses} Backups working fine
@{FullName=\\db-server\music\Ice Cube} Backups working fine
@{FullName=\\db-server\music\Jack Johnson} Backups working fine
@{FullName=\\db-server\music\Jake Bugg} Backups working fine
@{FullName=\\db-server\music\Jamie T} Backups working fine
@{FullName=\\db-server\music\Jay-Z} Backups working fine
@{FullName=\\db-server\music\Jefferson Airplane} Backups working fine
@{FullName=\\db-server\music\JME} Backups working fine
@{FullName=\\db-server\music\Kaiser Chiefs} Backups working fine
@{FullName=\\db-server\music\Kanye West} Backups working fine
@{FullName=\\db-server\music\Kendrick Lamar} Backups working fine
@{FullName=\\db-server\music\Lil Yachty} Backups working fine
@{FullName=\\db-server\music\Limp Bizkit} Backups working fine
@{FullName=\\db-server\music\Linkin Park} Backups working fine
@{FullName=\\db-server\music\Lionel Richie & The Commodores} Backups working fine
@{FullName=\\db-server\music\Madness} Backups working fine
@{FullName=\\db-server\music\Major Lazer} Backups working fine
@{FullName=\\db-server\music\Men at Work} Backups working fine
@{FullName=\\db-server\music\Metallica} Backups working fine
@{FullName=\\db-server\music\Milky Chance} Backups working fine
@{FullName=\\db-server\music\Ministry of Sound} Backups working fine
@{FullName=\\db-server\music\Muse} Backups working fine
@{FullName=\\db-server\music\N.W.A} Backups working fine
@{FullName=\\db-server\music\Neil Diamond} Backups working fine
@{FullName=\\db-server\music\Nirvana} Backups working fine
@{FullName=\\db-server\music\Phil Collins} Backups working fine
@{FullName=\\db-server\music\Red Hot Chilli Peppers} Backups working fine
@{FullName=\\db-server\music\Rudimental} Backups working fine
@{FullName=\\db-server\music\Run-D.M.C} Backups working fine
@{FullName=\\db-server\music\Simple Minds} Backups working fine
@{FullName=\\db-server\music\Skepta} Backups working fine
@{FullName=\\db-server\music\Snatch - Original Film Soundtrack} Backups working fine
@{FullName=\\db-server\music\Snoop Dogg} Backups working fine
@{FullName=\\db-server\music\Spandau Ballett} Backups working fine
@{FullName=\\db-server\music\Steely Dan} Backups working fine
@{FullName=\\db-server\music\The Hollies} Backups working fine
@{FullName=\\db-server\music\The Kooks} Backups working fine
@{FullName=\\db-server\music\The Notorious B.I.G} Backups working fine
@{FullName=\\db-server\music\The Weekend} Backups working fine
@{FullName=\\db-server\music\Tyler, the Creator} Backups working fine
@{FullName=\\db-server\music\Wiley} Backups working fine
@{FullName=\\db-server\music\Wilkinson} Backups working fine
@{FullName=\\db-server\music\Wiz Khalifa} Backups working fine

它显示列表中的所有文件夹,但没有一个显示为“过时”。在运行脚本之前,我确保某些文件夹在过去 4 天内肯定已被修改。

这是我第一次使用 powershell,所以如果我遗漏了一些非常明显的东西,我深表歉意。我已经尝试了几个小时,但我无法解决。

任何帮助将不胜感激!

4

2 回答 2

1

这里的这条线会导致问题

if ($path | Where{$path.LastWriteTime -ge (get-date).AddDays(-$4)})

应该-$4只是。-4由于其中有$一个空变量 $4,因此它正在查看当前日期,而不是您想要的 4 天前。

这将解决您的问题,但 boxdog 的回答对您的最终目标有另一个很好的建议。

于 2018-04-12T20:34:55.560 回答
1

这是您的代码的修改版本,它输出一组对象,每个目录一个,目录名称 LastWriteTime 以及它们是否是最新的:

Get-Childitem -Path "\\server\backups\customer1\" -Directory |
    ForEach-Object {$threshold = (Get-Date).AddDays(-4)}{
        [PsCustomObject]@{
            DirectoryName = "$($_.FullName)";
            LastWriteTime = "$($_.LastWriteTime)";
            BackupsAreRecent = "$($_.LastWriteTime -ge $threshold)"
        }
    }

您可以进一步处理对象(例如,按 LastWriteTime 对它们进行排序),或者您将获得如下表格:

DirectoryName                        LastWriteTime       BackupsAreRecent
-------------                        -------------       ----------------
\\server\backups\customer1\Dir1      04/10/2018 13:29:32 True
\\server\backups\customer1\Dir2      01/10/2018 13:29:32 False
于 2018-04-12T20:46:14.517 回答