Skip to content

Commit ef7951e

Browse files
committed
Get-Tail
1 parent 43ae1e8 commit ef7951e

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Examples/Example1.png

44.6 KB
Loading

Get-Tail.ps1

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#requires -Version 3
2+
3+
function Get-Tail
4+
{
5+
<#
6+
.SYNOPSIS
7+
Tail command
8+
.DESCRIPTION
9+
Can tail files and wait on multiple files unlike Get-Content -Wait
10+
.PARAMETER Path
11+
Paths to log files
12+
.PARAMETER Tail
13+
number of lines to tail (0 for none)
14+
.PARAMETER RefreshRate
15+
Refresh interval
16+
#>
17+
[CmdletBinding()]
18+
param
19+
(
20+
[Parameter(Mandatory, Position = 0,ValueFromPipeline=$true)]
21+
[string[]]$Path,
22+
23+
[Parameter(Position = 1)]
24+
[Int32]$Tail = 10,
25+
26+
[Parameter(Position = 2)]
27+
[Int32]$RefreshRate = 100
28+
)
29+
30+
#check if pipeline or path variable
31+
if ($Input)
32+
{
33+
[string[]]$Paths = $Input
34+
}
35+
else
36+
{
37+
$Paths = $Path
38+
}
39+
40+
#create Readers
41+
$Files = @{}
42+
foreach ($Path in $Paths)
43+
{
44+
$Reader = New-Object -TypeName System.IO.StreamReader -ArgumentList (New-Object -TypeName IO.FileStream -ArgumentList ($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [IO.FileShare]::ReadWrite))
45+
#get end the file
46+
$LastMaxOffset = $Reader.BaseStream.Length
47+
#get encoding
48+
$null = $Reader.Readline()
49+
$Reader.DiscardBufferedData()
50+
#seek to the last max offset
51+
$null = $Reader.BaseStream.Seek($LastMaxOffset, [System.IO.SeekOrigin]::Begin)
52+
53+
$Files.Add($Path, (New-Object -TypeName PSObject -Property (@{
54+
'Reader' = $Reader
55+
'LastMaxOffset' = $LastMaxOffset
56+
})))
57+
}
58+
59+
#tail existing lines
60+
if ($Tail -gt 0)
61+
{
62+
foreach ($Path in $Paths)
63+
{
64+
Write-Host -ForegroundColor Green -Object $Path
65+
Get-Content -Path $Path -Tail $Tail
66+
}
67+
$LastFile = $Path
68+
}
69+
else
70+
{
71+
$LastFile = $null
72+
}
73+
74+
#tail new lines
75+
:ReaderLoop while ($true)
76+
{
77+
Start-Sleep -Milliseconds $RefreshRate
78+
foreach ($File in $Files.GetEnumerator())
79+
{
80+
#if the file size has not changed, idle
81+
if ($File.Value.Reader.BaseStream.Length -eq $File.Value.LastMaxOffset)
82+
{
83+
continue
84+
}
85+
86+
if ($LastFile -ne $File.name)
87+
{
88+
Write-Host -ForegroundColor Green -Object $File.name
89+
$LastFile = $File.name
90+
}
91+
92+
#read out of the file until the EOF
93+
while (($Line = $File.Value.Reader.ReadLine()) -ne $null)
94+
{
95+
$Line
96+
}
97+
#update the last max offset
98+
$File.Value.LastMaxOffset = $File.Value.Reader.BaseStream.Position
99+
}
100+
}
101+
}
102+
103+
<#
104+
Get-Tail -Path C:\Windows\WindowsUpdate.log,C:\Windows\win.ini
105+
Get-ChildItem -Path C:\Windows\win.ini,C:\Windows\*.log -Exclude PFRO.log | Get-Tail -Tail 5
106+
#>

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ Get-PaddedCenter -Title 'End',(Get-Date)
2222
Get-PaddedCenter -Title 'Test' -Separator $null -Width 8 -Pad ([char]35)
2323
##Test##
2424
```
25+
26+
## Get-Tail
27+
tail files and wait, support multiple files unlike Get-Content -Wait
28+
##### Examples
29+
```PowerShell
30+
Get-Tail -Path C:\Windows\WindowsUpdate.log,C:\Windows\win.ini
31+
Get-ChildItem -Path C:\Windows\win.ini,C:\Windows\*.log -Exclude PFRO.log | Get-Tail -Tail 5
32+
```
33+
![](https://raw.githubusercontent.com/ili101/PowerShell/master/Examples/Example1.png)

0 commit comments

Comments
 (0)