-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo-tail.ps1
72 lines (62 loc) · 1.95 KB
/
do-tail.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#----------------------------------------------------------------
# Tail.ps1
# Created by Joe Pruitt :
# http://devcentral.f5.com/weblogs/Joe/archive/2009/04/22/unix-to-powershell---tail.aspx
#----------------------------------------------------------------
function Tail()
{
param
(
[string]$filespec = $null,
[int]$num_bytes = -1,
[int]$num_lines = -1,
[bool]$follow = $true,
[int]$sleep = 1,
[bool]$quiet = $false
);
# if no bytes or lines specified, default to 10 lines
if ( (-1 -eq $num_bytes) -and (-1 -eq $num_lines) ) { $num_lines = 10; }
$files = @(Get-ChildItem $filespec);
foreach ($file in $files)
{
# Optionally output file names when multiple files given
if ( ($files.Length -gt 1) -and !$quiet ) { Write-Host "==> $($file.Name) <=="; }
if ( -1 -ne $num_lines )
{
$prev_len = 0;
while ($true)
{
# For line number option, get content as an array of lines
# and print out the last "n" of them.
$lines = Get-Content $file;
if ( $prev_len -ne 0 ) { $num_lines = $lines.Length - $prev_len; }
$start_line = $lines.Length - $num_lines;
# Ensure that we don't go past the beginning of the input
if ( $start_line -le 0 ) { $start_line = 0; }
for ($i = $start_line; $i -lt $lines.Length; $i++)
{
$lines[$i];
}
$prev_len = $lines.Length;
# If we are following the file, sleep the desired interval
# else break out of the loop and continue with the next file.
if ( $follow )
{
Start-Sleep $sleep;
}
else
{
break;
}
}
}
elseif ( -1 -ne $num_bytes )
{
# for num bytes option, get the content as a single string
# and substring the last "n" bytes.
[string]$content = Get-Content $file -delim [char]0;
if ( ($content.Length - $num_bytes) -lt 0 ) { $num_bytes = $content.Length; }
$content.SubString($content.Length - $num_bytes);
}
}
}