Skip to content

Commit 0a6c02b

Browse files
committed
fix: bat and ps1 source
1 parent 8ba395b commit 0a6c02b

File tree

2 files changed

+68
-60
lines changed

2 files changed

+68
-60
lines changed

Diff for: scripts/thunderstorm-collector.bat

+11-7
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ SET DEBUG=0
4848

4949
:: Source
5050
SET SOURCE=
51-
IF "%SOURCE%"=="" (
52-
FOR /F "tokens=*" %%i IN ('hostname') DO SET HOSTNAME=%%i
53-
)
54-
IF "%SOURCE%" EQ "" AND "%HOSTNAME%" NEQ "" (
55-
SOURCE="?source=%HOSTNAME%"
56-
)
5751

5852
:: WELCOME -------------------------------------------------------
5953

@@ -83,10 +77,20 @@ ECHO Cannot find curl in PATH or the current directory. Download it from https:/
8377
ECHO If you're collecting on Windows systems older than Windows Vista, use curl version 7.46.0 from https://bintray.com/vszakats/generic/download_file?file_path=curl-7.46.0-win32-mingw.7z
8478
EXIT /b 1
8579
:CHECKDONE
86-
ECHO Curl has been found. We're ready to go.
80+
ECHO Curl has been found. We're ready to go.
8781

8882
:: COLLECTION --------------------------------------------------
8983

84+
:: SOURCE
85+
IF "%SOURCE%"=="" (
86+
ECHO Getting hostname
87+
FOR /F "tokens=*" %%i IN ('hostname') DO SET SOURCE=%%i
88+
ECHO No Source provided, using hostname=!SOURCE!
89+
)
90+
IF "%SOURCE%" NEQ "" (
91+
SET SOURCE=?source=%SOURCE%
92+
)
93+
9094
:: Directory walk and upload
9195
ECHO Processing %COLLECT_DIRS% with filters MAX_SIZE: %COLLECT_MAX_SIZE% MAX_AGE: %MAX_AGE% days EXTENSIONS: %RELEVANT_EXTENSIONS%
9296
ECHO This could take a while depending on the disk size and number of files. (set DEBUG=1 to see all skips)

Diff for: scripts/thunderstorm-collector.ps1

+57-53
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Date Created: 07.10.2020
77
# Last Modified: 07.10.2020
88
##################################################
9-
9+
1010
#Requires -Version 3
1111

1212
<#
@@ -48,47 +48,47 @@
4848
# Parameters ----------------------------------------------------------
4949
# #####################################################################
5050

51-
param
52-
(
53-
[Parameter(
54-
HelpMessage='Server name (FQDN) or IP address of your Thunderstorm instance')]
55-
[ValidateNotNullOrEmpty()]
51+
param
52+
(
53+
[Parameter(
54+
HelpMessage='Server name (FQDN) or IP address of your Thunderstorm instance')]
55+
[ValidateNotNullOrEmpty()]
5656
[Alias('TS')]
57-
[string]$ThunderstormServer,
57+
[string]$ThunderstormServer,
5858

59-
[Parameter(HelpMessage="Port number on which the Thunderstorm service is listening (default: 8080)")]
60-
[ValidateNotNullOrEmpty()]
61-
[Alias('TP')]
59+
[Parameter(HelpMessage="Port number on which the Thunderstorm service is listening (default: 8080)")]
60+
[ValidateNotNullOrEmpty()]
61+
[Alias('TP')]
6262
[int]$ThunderstormPort = 8080,
6363

64-
[Parameter(HelpMessage="")]
65-
[Alias('S')]
64+
[Parameter(HelpMessage="Source of the submission (default: hostname of the system)")]
65+
[Alias('S')]
6666
[string]$Source=$env:COMPUTERNAME,
6767

68-
[Parameter(HelpMessage="Folder to process (default: C:\)")]
69-
[ValidateNotNullOrEmpty()]
68+
[Parameter(HelpMessage="Folder to process (default: C:\)")]
69+
[ValidateNotNullOrEmpty()]
7070
[Alias('F')]
7171
[string]$Folder = "C:\",
72-
73-
[Parameter(
74-
HelpMessage='Select files based on the number of days in which the file has been create or modified (default: 0 = no age selection)')]
75-
[ValidateNotNullOrEmpty()]
76-
[Alias('MA')]
77-
[int]$MaxAge,
78-
79-
[Parameter(
80-
HelpMessage='Select only files smaller than the given number in MegaBytes (default: 20MB) ')]
81-
[ValidateNotNullOrEmpty()]
82-
[Alias('MS')]
83-
[int]$MaxSize,
84-
85-
[Parameter(HelpMessage='Extensions to select for submission (default: all of them)')]
86-
[ValidateNotNullOrEmpty()]
87-
[Alias('E')]
88-
[string[]]$Extensions,
89-
90-
[Parameter(HelpMessage='Enables debug output and skips cleanup at the end of the scan')]
91-
[ValidateNotNullOrEmpty()]
72+
73+
[Parameter(
74+
HelpMessage='Select files based on the number of days in which the file has been create or modified (default: 0 = no age selection)')]
75+
[ValidateNotNullOrEmpty()]
76+
[Alias('MA')]
77+
[int]$MaxAge,
78+
79+
[Parameter(
80+
HelpMessage='Select only files smaller than the given number in MegaBytes (default: 20MB) ')]
81+
[ValidateNotNullOrEmpty()]
82+
[Alias('MS')]
83+
[int]$MaxSize,
84+
85+
[Parameter(HelpMessage='Extensions to select for submission (default: all of them)')]
86+
[ValidateNotNullOrEmpty()]
87+
[Alias('E')]
88+
[string[]]$Extensions,
89+
90+
[Parameter(HelpMessage='Enables debug output and skips cleanup at the end of the scan')]
91+
[ValidateNotNullOrEmpty()]
9292
[Alias('D')]
9393
[switch]$Debugging = $False
9494
)
@@ -150,19 +150,19 @@ if ( $Args.Count -eq 0 -and $ThunderstormServer -eq "" ) {
150150
function Write-Log {
151151
param (
152152
[Parameter(Mandatory=$True, Position=0, HelpMessage="Log entry")]
153-
[ValidateNotNullOrEmpty()]
153+
[ValidateNotNullOrEmpty()]
154154
[String]$Entry,
155155

156-
[Parameter(Position=1, HelpMessage="Log file to write into")]
157-
[ValidateNotNullOrEmpty()]
158-
[Alias('SS')]
156+
[Parameter(Position=1, HelpMessage="Log file to write into")]
157+
[ValidateNotNullOrEmpty()]
158+
[Alias('SS')]
159159
[IO.FileInfo]$LogFile = "thunderstorm-collector.log",
160160

161161
[Parameter(Position=3, HelpMessage="Level")]
162-
[ValidateNotNullOrEmpty()]
162+
[ValidateNotNullOrEmpty()]
163163
[String]$Level = "Info"
164164
)
165-
165+
166166
# Indicator
167167
$Indicator = "[+]"
168168
if ( $Level -eq "Warning" ) {
@@ -185,7 +185,7 @@ function Write-Log {
185185
} else {
186186
Write-Host "$($Indicator) $($Entry)"
187187
}
188-
188+
189189
# Log File
190190
if ( $global:NoLog -eq $False ) {
191191
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') $($env:COMPUTERNAME): $Entry" | Out-File -FilePath $LogFile -Append
@@ -225,27 +225,31 @@ if ( $AutoDetectPlatform -ne "" ) {
225225
}
226226

227227
# URL Creation
228-
$Url = "http://$($ThunderstormServer):$($ThunderstormPort)/api/checkAsync"
228+
if ( $Source -ne "" ) {
229+
Write-Log "Using Source: $($Source)"
230+
$SourceParam = "?Source=$Source"
231+
}
232+
$Url = "http://$($ThunderstormServer):$($ThunderstormPort)/api/checkAsync$($SourceParam)"
229233
Write-Log "Sending to URI: $($Url)" -Level "Debug"
230234

231235
# ---------------------------------------------------------------------
232236
# Run THOR Thunderstorm Collector -------------------------------------
233237
# ---------------------------------------------------------------------
234238
$ProgressPreference = "SilentlyContinue"
235239
try {
236-
Get-ChildItem -Path $Folder -File -Recurse -ErrorAction SilentlyContinue |
240+
Get-ChildItem -Path $Folder -File -Recurse -ErrorAction SilentlyContinue |
237241
ForEach-Object {
238242
# -------------------------------------------------------------
239-
# Filter ------------------------------------------------------
243+
# Filter ------------------------------------------------------
240244
# Size Check
241245
if ( ( $_.Length / 1MB ) -gt $($MaxSize) ) {
242-
Write-Log "$_ skipped due to size filter" -Level "Debug"
246+
Write-Log "$_ skipped due to size filter" -Level "Debug"
243247
return
244248
}
245-
# Age Check
249+
# Age Check
246250
if ( $($MaxAge) -gt 0 ) {
247251
if ( $_.LastWriteTime -lt (Get-Date).AddDays(-$($MaxAge)) ) {
248-
Write-Log "$_ skipped due to age filter" -Level "Debug"
252+
Write-Log "$_ skipped due to age filter" -Level "Debug"
249253
return
250254
}
251255
}
@@ -259,7 +263,7 @@ try {
259263

260264
# -------------------------------------------------------------
261265
# Submission --------------------------------------------------
262-
266+
263267
Write-Log "Processing $($_.FullName) ..." -Level "Debug"
264268
# Reading the file data & preparing the request
265269
try {
@@ -270,12 +274,12 @@ try {
270274
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
271275
$boundary = [System.Guid]::NewGuid().ToString();
272276
$LF = "`r`n";
273-
$bodyLines = (
277+
$bodyLines = (
274278
"--$boundary",
275279
"Content-Disposition: form-data; name=`"file`"; filename=`"$($_.FullName)`"",
276280
"Content-Type: application/octet-stream$LF",
277281
$fileEnc,
278-
"--$boundary--$LF"
282+
"--$boundary--$LF"
279283
) -join $LF
280284

281285
# Submitting the request
@@ -286,7 +290,7 @@ try {
286290
Write-Log "Submitting to Thunderstorm server: $($_.FullName) ..." -Level "Info"
287291
$Response = Invoke-WebRequest -uri $($Url) -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
288292
$StatusCode = [int]$Response.StatusCode
289-
}
293+
}
290294
# Catch all non 200 status codes
291295
catch {
292296
$StatusCode = $_.Exception.Response.StatusCode.value__
@@ -310,8 +314,8 @@ try {
310314
}
311315
}
312316
}
313-
} catch {
314-
Write-Log "Unknown error during Thunderstorm Collection $_" -Level "Error"
317+
} catch {
318+
Write-Log "Unknown error during Thunderstorm Collection $_" -Level "Error"
315319
}
316320

317321
# ---------------------------------------------------------------------

0 commit comments

Comments
 (0)