[英]Get-ChildItem in an enormous directory and RAM usage
我已经在域控制器(SRV2012R2)上创建了PS脚本。
该脚本检查每个共享文件夹(映射的驱动器),以查看是否存在大于2GB的文件:
foreach($dir in $Dirs)
{
$files = Get-ChildItem $dir -Recurse
foreach ($item in $files)
{
#Check if $item.Size is greater than 2GB
}
}
我有以下问题:
共享中充斥着超过800GB的(子)文件夹,文件,其中大部分只是普通文件。
每当我运行脚本时,我都会看到运行脚本时CPU + RAM消耗大量内存(进入Get-Childitem
-line 5分钟后,RAM已达到4GB以上)。
我的问题是,为什么Get-ChildItem
需要这么多资源? 我可以使用什么替代方法? 因为我没有成功运行脚本。
我已经知道可以使用| SELECT fullname, length
| SELECT fullname, length
以我的Get-ChildItem
子句后的| SELECT fullname, length
为改进,但这完全没有帮助我(查询仍然消耗大量RAM)。
有什么我可以做的,以便我可以遍历目录而不会受到计算机资源的太大阻力?
与其将每个文件都保存到一个变量中,不如使用管道过滤掉不需要的文件(即小于2GB的文件):
foreach($dir in $Dirs)
{
$bigFiles = Get-ChildItem $dir -Recurse |Where Length -gt 2GB
}
如果您需要进一步处理或分析这些大文件,建议您使用ForEach-Object
扩展该管道:
foreach($dir in $Dirs)
{
Get-ChildItem $dir -Recurse |Where Length -gt 2GB |ForEach-Object {
# do whatever else you must
# $_ contains the current file
}
}
尝试这个:
# Create an Array
$BigFilesArray = @()
#Populate it with your data
$BigFilesArray = @(ForEach-Object($dir in $dirs) {Get-ChildItem $dir -Recurse | Where-Object Length -GT 2GB})
#Number of items in the array
$BigFilesArray.Count
#Looping to get the name and size of each item in the array
ForEach-Object ($bf in $BigFilesArray) {"$($bf.name) - $($bf.length)"}
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.