6
6
# Date Created: 07.10.2020
7
7
# Last Modified: 07.10.2020
8
8
# #################################################
9
-
9
+
10
10
# Requires -Version 3
11
11
12
12
<#
48
48
# Parameters ----------------------------------------------------------
49
49
# #####################################################################
50
50
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 ()]
56
56
[Alias (' TS' )]
57
- [string ]$ThunderstormServer ,
57
+ [string ]$ThunderstormServer ,
58
58
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' )]
62
62
[int ]$ThunderstormPort = 8080 ,
63
63
64
- [Parameter (HelpMessage = " " )]
65
- [Alias (' S' )]
64
+ [Parameter (HelpMessage = " Source of the submission (default: hostname of the system) " )]
65
+ [Alias (' S' )]
66
66
[string ]$Source = $env: COMPUTERNAME ,
67
67
68
- [Parameter (HelpMessage = " Folder to process (default: C:\)" )]
69
- [ValidateNotNullOrEmpty ()]
68
+ [Parameter (HelpMessage = " Folder to process (default: C:\)" )]
69
+ [ValidateNotNullOrEmpty ()]
70
70
[Alias (' F' )]
71
71
[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 ()]
92
92
[Alias (' D' )]
93
93
[switch ]$Debugging = $False
94
94
)
@@ -150,19 +150,19 @@ if ( $Args.Count -eq 0 -and $ThunderstormServer -eq "" ) {
150
150
function Write-Log {
151
151
param (
152
152
[Parameter (Mandatory = $True , Position = 0 , HelpMessage = " Log entry" )]
153
- [ValidateNotNullOrEmpty ()]
153
+ [ValidateNotNullOrEmpty ()]
154
154
[String ]$Entry ,
155
155
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' )]
159
159
[IO.FileInfo ]$LogFile = " thunderstorm-collector.log" ,
160
160
161
161
[Parameter (Position = 3 , HelpMessage = " Level" )]
162
- [ValidateNotNullOrEmpty ()]
162
+ [ValidateNotNullOrEmpty ()]
163
163
[String ]$Level = " Info"
164
164
)
165
-
165
+
166
166
# Indicator
167
167
$Indicator = " [+]"
168
168
if ( $Level -eq " Warning" ) {
@@ -185,7 +185,7 @@ function Write-Log {
185
185
} else {
186
186
Write-Host " $ ( $Indicator ) $ ( $Entry ) "
187
187
}
188
-
188
+
189
189
# Log File
190
190
if ( $global :NoLog -eq $False ) {
191
191
" $ ( 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 "" ) {
225
225
}
226
226
227
227
# 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 ) "
229
233
Write-Log " Sending to URI: $ ( $Url ) " - Level " Debug"
230
234
231
235
# ---------------------------------------------------------------------
232
236
# Run THOR Thunderstorm Collector -------------------------------------
233
237
# ---------------------------------------------------------------------
234
238
$ProgressPreference = " SilentlyContinue"
235
239
try {
236
- Get-ChildItem - Path $Folder - File - Recurse - ErrorAction SilentlyContinue |
240
+ Get-ChildItem - Path $Folder - File - Recurse - ErrorAction SilentlyContinue |
237
241
ForEach-Object {
238
242
# -------------------------------------------------------------
239
- # Filter ------------------------------------------------------
243
+ # Filter ------------------------------------------------------
240
244
# Size Check
241
245
if ( ( $_.Length / 1 MB ) -gt $ ($MaxSize ) ) {
242
- Write-Log " $_ skipped due to size filter" - Level " Debug"
246
+ Write-Log " $_ skipped due to size filter" - Level " Debug"
243
247
return
244
248
}
245
- # Age Check
249
+ # Age Check
246
250
if ( $ ($MaxAge ) -gt 0 ) {
247
251
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"
249
253
return
250
254
}
251
255
}
@@ -259,7 +263,7 @@ try {
259
263
260
264
# -------------------------------------------------------------
261
265
# Submission --------------------------------------------------
262
-
266
+
263
267
Write-Log " Processing $ ( $_.FullName ) ..." - Level " Debug"
264
268
# Reading the file data & preparing the request
265
269
try {
@@ -270,12 +274,12 @@ try {
270
274
$fileEnc = [System.Text.Encoding ]::GetEncoding(' UTF-8' ).GetString($fileBytes );
271
275
$boundary = [System.Guid ]::NewGuid().ToString();
272
276
$LF = " `r`n " ;
273
- $bodyLines = (
277
+ $bodyLines = (
274
278
" --$boundary " ,
275
279
" Content-Disposition: form-data; name=`" file`" ; filename=`" $ ( $_.FullName ) `" " ,
276
280
" Content-Type: application/octet-stream$LF " ,
277
281
$fileEnc ,
278
- " --$boundary --$LF "
282
+ " --$boundary --$LF "
279
283
) -join $LF
280
284
281
285
# Submitting the request
@@ -286,7 +290,7 @@ try {
286
290
Write-Log " Submitting to Thunderstorm server: $ ( $_.FullName ) ..." - Level " Info"
287
291
$Response = Invoke-WebRequest - uri $ ($Url ) - Method Post - ContentType " multipart/form-data; boundary=`" $boundary `" " - Body $bodyLines
288
292
$StatusCode = [int ]$Response.StatusCode
289
- }
293
+ }
290
294
# Catch all non 200 status codes
291
295
catch {
292
296
$StatusCode = $_.Exception.Response.StatusCode.value__
@@ -310,8 +314,8 @@ try {
310
314
}
311
315
}
312
316
}
313
- } catch {
314
- Write-Log " Unknown error during Thunderstorm Collection $_ " - Level " Error"
317
+ } catch {
318
+ Write-Log " Unknown error during Thunderstorm Collection $_ " - Level " Error"
315
319
}
316
320
317
321
# ---------------------------------------------------------------------
0 commit comments