66# Date Created: 07.10.2020
77# Last Modified: 07.10.2020
88# #################################################
9-
9+
1010# Requires -Version 3
1111
1212<#
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 "" ) {
150150function 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 ) "
229233Write-Log " Sending to URI: $ ( $Url ) " - Level " Debug"
230234
231235# ---------------------------------------------------------------------
232236# Run THOR Thunderstorm Collector -------------------------------------
233237# ---------------------------------------------------------------------
234238$ProgressPreference = " SilentlyContinue"
235239try {
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 / 1 MB ) -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