-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTlog.ps1
More file actions
78 lines (71 loc) · 2.19 KB
/
BTlog.ps1
File metadata and controls
78 lines (71 loc) · 2.19 KB
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
73
74
75
76
77
78
#Requires –Version 3
Function loadConfig($path)
{
$global:appSettings = @{}
$config = [xml](get-content $path)
foreach ($addNode in $config.configuration.appsettings.add) {
if ($addNode.Value.Contains(‘,’)) {
# Array case
$value = $addNode.Value.Split(‘,’)
for ($i = 0; $i -lt $value.length; $i++) {
$value[$i] = $value[$i].Trim()
}
}
else {
# Scalar case
$value = $addNode.Value
}
$global:appSettings[$addNode.Key] = $value
}
}
function execCmd($cmdStr)
{
$webclient = new-object System.Net.WebClient
try {
$response = $webclient.DownloadString("http://$($appSettings["BTnicAddress"])/btnic.cgi?$cmdStr")
$response = $response | ConvertFrom-JSON
if ($appSettings["TimestampFormat"]) {
$timestamp = get-date -f $appSettings["TimestampFormat"]
$response = @($timestamp) + $response
}
return $response
} catch {
throw
}
}
function Get-StartFileTimestamp () {
if ($appSettings["TimestampFormat"]) {
return [DateTime]::ParseExact((get-content $appSettings["LogFileName"] -TotalCount 1).split("`t")[0], $appSettings["TimestampFormat"], [CultureInfo]::InvariantCulture)
} else {
return (Get-ItemProperty $appSettings["LogFileName"]).CreationTime
}
}
function Test-LogArchive ($lastResponse) {
if ((Test-Path $appSettings["LogFileName"]) -And $appSettings["NewLogFileTimeout"] -And ((Get-Date) - $lastResponse -gt [TimeSpan]$appSettings["NewLogFileTimeout"])) {
$startTimeStamp = Get-StartFileTimestamp
Move-Item -Path $appSettings["LogFileName"] -Destination ($appSettings["ArchiveLogPath"] + $startTimestamp.ToString($appSettings["ArchiveLogFileName"]))
}
}
loadConfig BTlog.config
if (Test-Path $appSettings["LogFileName"]) {
$lastResponse = (Get-ItemProperty $appSettings["LogFileName"]).LastWriteTime
} else {
$lastResponse = Get-Date
}
Test-LogArchive ($lastResponse)
while ($true) {
$appSettings["LogItems"] | ForEach-Object {
try {
$response = execCmd -cmdStr $_
if ($response) {
[string]::join("`t", $response) | out-file -append -encoding ASCII $appSettings["LogFileName"]
$lastResponse = Get-Date
}
} catch {
Test-LogArchive ($lastResponse)
}
}
if ($appSettings["LogInterval"]) {
Start-Sleep -s $appSettings["LogInterval"]
}
}