function Start-FileTail {
param($path)
# Get unique source ID
$sourceID = "FileTailLine-" + [guid]::NewGuid()
$job = Start-Job -ArgumentList $path, $sourceID {
param($path,$sid)
Register-EngineEvent -SourceIdentifier $sid -Forward
do{}until(Test-Path $path)
$fs = New-Object IO.FileStream ($path, [IO.FileMode]::Open,
[IO.FileAccess]::Read, [IO.FileShare]::ReadWrite)
$sr = New-Object IO.StreamReader ($fs)
$lines = @()
while(1) {
$line = $sr.ReadLine()
$lines += $line
# Send after every 100 reads
if($lines.Count -gt 100) {
# Join lines into 1 string
$text = @($lines| where {$_}) -join "`n"
# Only send if text was found
if($text){New-Event -SourceIdentifier $sid -MessageData $text}
$lines = @()
}
}
}
$event = Register-EngineEvent -SourceIdentifier $sourceID -Action {
Write-Host $event.MessageData
}
New-Object Object|
Add-Member -Name Job -Type NoteProperty -Value $job -PassThru|
Add-Member -Name SourceIdentifier -Type NoteProperty -Value $sourceID -PassThru
}
function Stop-FileTail {
param($TailInfo)
Remove-Job $TailInfo.Job -Force
Unregister-Event -SourceIdentifier $tail.SourceIdentifier
}
您可以删除的作业,一旦安装完成注销事件。
变化Write-Host
到Write-Verbose
为-Verbose
支持
编辑:我测试了我的答案,同时安装应用程序,并发现它读取日志文件时非常慢。我更新了Get-Content
调用,使用-ReadCount 100
作为行数组发送数据。 Write-Host
行已更新以处理阵列。
我还发现使用-Wait
开关Start-Process
导致所有日志输出在安装完成后写入。这可以固定使用:
$msi = Start-Process -FilePath "msiexec" -ArgumentList $p -PassThru
do{}until($msi.HasExited)
编辑2:嗯,我没有当我使用-Wait
和-ReadCount
一起得到所有的日志文件。我把日志文件的阅读放回原来的样子。我不确定如何处理速度。
编辑3:我更新了代码以使用StreamReader而不是Get-Content
,并将代码放入函数中。你会这样称呼它:
$path = "$ENV:TEMP\$name.log"
if(Test-Path $path){Remove-Item $path}
$msi = Start-Process -FilePath "msiexec" -ArgumentList $p -PassThru
$tail = Start-FileTail $p
do{}until($msi.HasExited)
sleep 1 # Allow time to finish reading log.
Stop-FileTail $tail
哇,感谢所有的工作!今晚我可能有时间来实施这个。我会定期检查:) –
仍然没有尝试过解决方案,它是为了在工作中的一个侧面项目,并且你不知道它,我的主要责任继续妨碍:)但是,你已经等待足够长的代表! –