小编MaC*_*ban的帖子

Powershell:单个脚本来“清理”多个文件夹的旧文件

我想创建一个维护脚本,该脚本将在我们的每台服务器上运行以清除常见的 drop/archive 目录。脚本最好将参考文件用于文件夹路径和所需的时效限制。然后脚本将清除超过老化限制的文件路径。输入参考文件如下所示:

c:\logs\iis\siteA\ 30
c:\logs\job1\ 60
e:\archive\clientA\ 90
Run Code Online (Sandbox Code Playgroud)

第一个组件是文件路径;第二个是文件应该保留的天数,用空格隔开。

现在对于脚本,我有以下内容;然而,我尴尬地缺乏脚本经验。(我更注重网络)

#Attempted loop
Foreach ($ParentDir = Get-Content .\paths.txt $arg1, $arg2)
{$limit = (Get-Date).AddDays(-$arg2)
$path = $ParentDir

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force}
Run Code Online (Sandbox Code Playgroud)

我觉得逻辑很接近,但我的语法关闭了。我的方法可行吗?有没有更好的办法?你能帮忙解释一下语法吗?使用 poweshell v3。

我从这篇文章中从deadlydog那里得到了很多逻辑。

scripting windows powershell filesystems powershell-v3.0

3
推荐指数
1
解决办法
2万
查看次数