我有许多存储在网络共享上的客户备份链,我想创建一个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,所以如果我错过了一些令人眼花缭乱的东西,我会道歉。我已经尝试了好几个小时,但我无法解决这个问题。
任何帮助都将受到大力赞赏!
答案 0 :(得分:1)
这一行会导致问题
if ($path | Where{$path.LastWriteTime -ge (get-date).AddDays(-$4)})
-$4
应为-4
。由于那里有一个$
,因此它会查看一个空变量$ 4,因此它会查看当前日期,而不是之前的4天。
这将解决您的问题,但是对于您的最终目标,boxdog的答案还有另一个很好的建议。
答案 1 :(得分: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