- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
PowerShell 新手,边做边学。
我创建的进程可以工作,但它最终会锁定我的机器直到完成,耗尽所有内存。我认为我通过强制垃圾收集器解决了这个问题,并且从 for-each 语句转向使用 %()
循环遍历所有内容。
流程快速概要:需要将多个 SharePoint 日志文件合并为单个日志文件,以跟踪所有公司不同 SharePoint 网站的使用情况。 PowerShell 循环遍历 SP 服务器上的所有日志目录,并检查目录中的每个文件是否已存在于我的本地计算机上。如果确实存在,则会附加文件文本,否则会直接复制。对 SharePoint 日志服务器上的每个文件和目录重复进行冲洗。在每个循环之间,我强制执行 GC,因为...因为我的基本理解是循环变量保存在内存中,并且我想刷新它们。我可能看错了。这是有问题的脚本。
$FinFiles = 'F:\Monthly Logging\Logs'
dir -path '\\SP-Log-Server\Log-Directory' | ?{$_.PSISContainer} | %{
$CurrentDir = $_
dir $CurrentDir.FullName | ?(-not $_.PSISContainer} | %{
if($_.Extension -eq ".log"){
$DestinationFile = $FinFiles + '\' + $_.Name
if((Test-Path $DestinationFile) -eq $false){
New-Item -ItemType file -path $DestinationFile -Force
Copy-Item $_.FullName $DestinationFile
}
else{
$A = Get-Content $_.FullName ; Add-Content $DestinationFile $A
Write-Host "Log File"$_.FullName"merged."
}
[GC]::Collect()
}
[GC]::Collect()
}
当然,已完成/附加的日志文件变得非常非常大(最小 300 MB,最大 1GB)。我是不是没有关闭一些我应该关闭的东西,或者在内存中保留一些开放的东西? (目前它占我 8 Gig 内存总量的 7.5。)
提前致谢。
最佳答案
不要像这样嵌套 Get-ChildItem
命令。请改用通配符。尝试:dir "\\SP-Log-Server\Log-Directory\*\*.log"
。这应该会改善事情的开始。然后将其移至 ForEach($X in $Y){}
循环,而不是 ForEach-Object{}
循环(您现在使用的循环)。我敢打赌这可以解决您的问题。
所以,我凭空重写了一遍:
$FinFiles = 'F:\Monthly Logging\Logs'
ForEach($LogFile in (dir -path '\\SP-Log-Server\Log-Directory\*\*.log')){
$DestinationFile = $FinFiles + '\' + $LogFile.Name
if((Test-Path $DestinationFile) -eq $false){
New-Item -ItemType file -path $DestinationFile -Force
Copy-Item $LogFile.FullName $DestinationFile
}
else{
$A = Get-Content $LogFile.FullName ; Add-Content $DestinationFile $A
Write-Host "Log File"$LogFile.FullName"merged."
}
}
}
编辑:哦,对了,Alexander Obersht 可能也很正确。您也可能会从 StreamReader 方法中受益。至少您应该使用 Get-Content
的 -readcount
参数,并且没有理由将其保存为变量,只需将其直接通过管道传递给 添加内容
cmdlet。
Get-Content $LogFile.FullName -ReadCount 5000| Add-Content $DestinationFile
为了进一步解释我的答案,如果您在管道中使用 ForEach-Object
,它会将所有内容保留在内存中(无论您的 GC 调用如何)。使用 ForEach
循环不会执行此操作,并且应该可以解决您的问题。
关于PowerShell内存泄漏误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703764/
所以我实际上正在阅读有关用于删除对象属性的“delete”方法的文档,并且我偶然发现了这个“此外,您不能删除使用 var 关键字声明的全局对象的属性。” 所以我真正理解的是:假设你创建了一个数组: v
我目前正在学习 Javascript,我想知道为什么下面的代码会执行“console.log('why')”。我认为“变量”(var、let、const)仅存储信息,不能自行执行。我不希望以下内容实际
在许多博客和引用文献中,我读到了有关冲洗刷新记录的信息,其中有很多信息会引起头痛和一些误解: 是否为复制的碎片保留了事务日志?为什么? 如果GET /_cat/allocation包含转记录? 超过磁
我有下一个循环: rolling_average_delta_follower=[]; followers=[32,34,36,38,40,42,44,46,48,50,52,54,5
我遇到了多处理问题;我在 linux 2.6.36 上使用 python2.7。我知道使用更高级别的模块或库会容易得多,但我正在尝试使用较低级别的函数(os.fork() 和 os.exec*)以确保
有人可以向我解释一下,为什么这段代码不起作用。我正在浏览周围的一些问题,但找不到答案。可能是因为(大量)缺乏知识。 感谢您提供的任何帮助。 char** sentence = malloc(min);
我的目标是我想用一些 java 代码登录到一个站点,并在登录后做一些工作。(为了编写一些 java cooking 处理,我首先需要了解这一切实际上是如何工作的)问题是我不太清楚如何管理 cookie
给定以下非常简单的结构: struct A { int a; double b; }; (使用 Mac OS 10.9 - Xcode) 结构的大小是 16。我不明白为什么。为什么不是
我刚刚下载了基于 ASP.NET 5 的 music store (microsoft sample projct) 源代码。我不明白为什么 Microsoft 的开发人员在 Controller 中
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
我正在阅读 this doc并看到以下片段: The := syntax is shorthand for declaring and initializing a variable, e.g. fo
我在理解描述的 MixColumns 步骤时遇到问题 here . 我知道扩散,这一切都是有道理的,因为它指出每列都被视为多项式并乘以 GF(2^8) 的模。 但是..乘以GF(2 ^ 8)。尽管域仍
我尝试自定义HTML文件输入并编写了最简单的jquery API函数: (function($) { $.fn.selectFileInputGUI = function() {
我对 SSL/TLS 有一个根本性的误解,希望能得到澄清。 按照我的理解,当我获得网站证书时,它包含我的所有信息,并由我的证书颁发机构(VeriSign 或其他任何人)签名。当有人从我的网站请求使用
我们在代码中有一个 NamedTuple,如下所示: from typing import NamedTuple class StandardParameters(NamedTuple): o
我有一个问题,我需要你的帮助: 我正在制作一个 Web 应用程序来访问我需要在类似( ListView 或 DataList )这样的工具中显示他们的员工列表,以直接绑定(bind)数据库中的数据,并
我知道根是: 静态字段 方法参数 本地领域 f-queue 也包含一个指向“将要完成的”对象的指针 cpu 寄存器 <=??? 现在让我们谈谈寄存器。 它们可以包含的代码如下: mov bx, 003
官方例子Timer组件使用this.interval var Timer = React.createClass({ getInitialState: function() { retur
使用 PostGIS 我有两个表,第一个包含 250 个城市的边界,第二个包含世界上所有国家/地区的边界。 我试图影响每个城市所属的国家/地区。下面的查询可以让我得到我想要的结果。 SELECT DI
我正在准备数据库和 SQL 考试,并且正在解决一个练习: 我们有一个包含 4 个表的数据库,代表一家人力资源公司。这些表格是: applicant(a-id,a-name,a-city,years-
我是一名优秀的程序员,十分优秀!