-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-exporter.ps1
158 lines (132 loc) · 4.19 KB
/
run-exporter.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateScript({(Test-Path -LiteralPath $_ -PathType Leaf)})]
$ExecutablePath,
[Switch]
[bool] $BatchMode,
[Parameter()]
[ValidateScript({(Test-Path -LiteralPath $_ -PathType Container -IsValid)})]
[string] $RenderPath,
[Parameter()]
[ValidateRange(300, [int]::MaxValue)]
[int] $RenderHeight = 300,
[Parameter()]
[ValidateRange(300, [int]::MaxValue)]
[int] $RenderWidth = 300,
[Switch]
[bool] $Transmit,
[Switch]
[bool] $LogJson,
[Parameter()]
[ValidateScript({(Test-Path -LiteralPath $_ -PathType Container -IsValid)})]
[string] $JsonPath,
[Parameter()]
[ValidateRange(1, [int]::MaxValue)]
[int] $ExportCount = -1,
[Parameter()]
[ValidatePattern('^[0-9]+(s|m|)$')]
[string] $ExportDelay,
[Parameter()]
[ValidatePattern('^[0-9]+(s|m|)$')]
[string] $TotalExportTime,
[Parameter()]
[ushort] $Port,
[Parameter()]
[string] $Interface
)
[System.Diagnostics.Process]$exporter = New-Object System.Diagnostics.Process
$exporter.StartInfo.FileName = Resolve-Path -Path $ExecutablePath | Select-Object -ExpandProperty Path
if ($BatchMode)
{
$exporter.StartInfo.Arguments = "-batchmode -nographics"
}
$exporter.StartInfo.Arguments += " -logFile `"$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("./exporter.log"))`" export"
if ($RenderPath) {
$exporter.StartInfo.Arguments += " --renderPath `"$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$RenderPath"))`""
}
if ($JsonPath) {
$exporter.StartInfo.Arguments += " --writeToFile `"$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($JsonPath))`""
}
if ($Transmit) {
$exporter.StartInfo.Arguments += " --transmit"
}
if ($LogJson) {
$exporter.StartInfo.Arguments += " --logExport"
}
if ($ExportDelay -ne -1) {
$exporter.StartInfo.Arguments += " --delay $ExportDelay"
}
if ($ExportCount) {
$exporter.StartInfo.Arguments += " --exportCount $ExportCount"
}
if ($TotalExportTime) {
$exporter.StartInfo.Arguments += " --totalTime $TotalExportTime"
}
$exporter.StartInfo.Arguments += " -h $RenderHeight"
$exporter.StartInfo.Arguments += " -w $RenderWidth"
if ($Port)
{
$exporter.StartInfo.Arguments += " --port ${Port}"
}
if ($Interface)
{
$exporter.StartInfo.Arguments += " --interface `"${Interface}`""
}
Write-Verbose "Launching ${ExecutablePath} as exporter with the arguments: $($exporter.StartInfo.Arguments)"
try {
if (!$exporter.Start()) {
Write-Error "Failed to start exporter instance." -ErrorAction Stop
}
} catch {
Write-Error "Failed to find exporter executable." -ErrorAction Stop
}
Write-Host "Starting exporter instance..."
if (!($PSBoundParameters['Verbose']) -or ($VerbosePreference -eq 'Continue')) {
Write-Verbose "VERBOSE is set. Exporter log will be written to the console."
$exporterReader = Start-Job {
Get-Content ".\exporter.log" -Wait
}
}
[System.Console]::TreatControlCAsInput = $true
while (!$exporter.WaitForExit(500)) {
if ([System.Console]::KeyAvailable)
{
$key = [System.Console]::ReadKey($true)
if (($key.modifiers -band [System.ConsoleModifiers]"control") -and ($key.key -eq "C"))
{
break;
}
}
if ($exporterReader)
{
$exporterReader `
| Receive-Job `
| ForEach-Object {
$_ `
| Join-String -OutputPrefix "EXPORTER: " `
| Write-Verbose
}
}
}
if (!$exporter.HasExited) {
Write-Warning "Detected force exit. Killing instances."
$exporter.Kill()
Exit 1
} elseif ($exporter.ExitCode -ne 0) {
Write-Error "Exporter Failed to complete successfully."
Exit $exporter.ExitCode
} else {
if ($exporterReader) {
Start-Sleep 0.5 # try to get remaining output
Stop-Job $exporterReader
$exporterReader `
| Receive-Job `
| ForEach-Object {
$_ `
| Join-String -OutputPrefix "EXPORTER: " `
| Write-Verbose
}
Remove-Job $exporterReader
}
Write-Host "Exporter Instance has completed successfully."
}