@@ -111,8 +111,8 @@ param(
111
111
)
112
112
begin {
113
113
114
- if ($FalconAccessToken ){
115
- if ($FalconCloud -eq " autodiscover" ){
114
+ if ($FalconAccessToken ) {
115
+ if ($FalconCloud -eq " autodiscover" ) {
116
116
$Message = ' Unable to auto discover Falcon region using access token, please provide FalconCloud'
117
117
throw $Message
118
118
}
@@ -178,10 +178,10 @@ begin {
178
178
function Invoke-FalconAuth ([hashtable ] $WebRequestParams , [string ] $BaseUrl , [hashtable ] $Body , [string ] $FalconCloud ) {
179
179
$Headers = @ {' Accept' = ' application/json' ; ' Content-Type' = ' application/x-www-form-urlencoded' ; ' charset' = ' utf-8' }
180
180
$Headers.Add (' User-Agent' , ' crowdstrike-falcon-scripts/1.7.1' )
181
- if ($FalconAccessToken ){
181
+ if ($FalconAccessToken ) {
182
182
$Headers.Add (' Authorization' , " bearer $ ( $FalconAccessToken ) " )
183
183
}
184
- else {
184
+ else {
185
185
try {
186
186
$response = Invoke-WebRequest @WebRequestParams - Uri " $ ( $BaseUrl ) /oauth2/token" - UseBasicParsing - Method ' POST' - Headers $Headers - Body $Body
187
187
$content = ConvertFrom-Json - InputObject $response.Content
@@ -192,7 +192,7 @@ begin {
192
192
throw $Message
193
193
}
194
194
195
- if ($GetAccessToken -eq $true ){
195
+ if ($GetAccessToken -eq $true ) {
196
196
Write-Output $content.access_token | out-host
197
197
exit
198
198
}
@@ -366,17 +366,86 @@ begin {
366
366
}
367
367
}
368
368
process {
369
- if (([Security.Principal.WindowsPrincipal ] [Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole(
370
- [Security.Principal.WindowsBuiltInRole ]::Administrator) -eq $false ) {
371
- $Message = ' Unable to proceed without administrative privileges'
372
- throw $Message
369
+ if (! $GetAccessToken ) {
370
+ if (([Security.Principal.WindowsPrincipal ] [Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole(
371
+ [Security.Principal.WindowsBuiltInRole ]::Administrator) -eq $false ) {
372
+ $Message = ' Unable to proceed without administrative privileges'
373
+ throw $Message
374
+ }
375
+
376
+ $AgentService = Get-Service - Name CSAgent - ErrorAction SilentlyContinue
377
+ if (! $AgentService ) {
378
+ $Message = " 'CSFalconService' service not found, already uninstalled"
379
+ Write-FalconLog ' CheckService' $Message
380
+ break
381
+ }
373
382
}
383
+ # Check if credentials were provided
384
+ $AuthProvided = (Test-FalconCredential $FalconClientId $FalconClientSecret ) -or $FalconAccessToken
374
385
375
- $AgentService = Get-Service - Name CSAgent - ErrorAction SilentlyContinue
376
- if (! $AgentService ) {
377
- $Message = " 'CSFalconService' service not found, already uninstalled"
378
- Write-FalconLog ' CheckService' $Message
379
- break
386
+ if ($AuthProvided ) {
387
+ # TLS check should be first since it's needed for all HTTPS communication
388
+ if ([Net.ServicePointManager ]::SecurityProtocol -notmatch ' Tls12' ) {
389
+ try {
390
+ [Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
391
+ }
392
+ catch {
393
+ $message = $_
394
+ Write-FalconLog ' TlsCheck' $message
395
+ throw $message
396
+ }
397
+ }
398
+
399
+ # Hashtable for common Invoke-WebRequest parameters
400
+ $WebRequestParams = @ {}
401
+
402
+ # Configure proxy based on arguments
403
+ $proxy = " "
404
+ if ($ProxyHost ) {
405
+ Write-Output " Proxy settings detected in arguments, using proxy settings to communicate with the CrowdStrike api"
406
+
407
+ if ($ProxyHost ) {
408
+ $proxy_host = $ProxyHost.Replace (" http://" , " " ).Replace(" https://" , " " )
409
+ Write-FalconLog - Source " Proxy" - Message " Proxy host ${proxy_host} found in arguments" - stdout $true
410
+ }
411
+
412
+ if ($ProxyPort ) {
413
+ Write-FalconLog - Source " Proxy" - Message " Proxy port ${ProxyPort} found in arguments" - stdout $true
414
+ $proxy = " http://${proxy_host} :${ProxyPort} "
415
+ }
416
+ else {
417
+ $proxy = " http://${proxy_host} "
418
+ }
419
+
420
+ $proxy = $proxy.Replace (" '" , " " ).Replace(" `" " , " " )
421
+ Write-FalconLog - Source " Proxy" - Message " Using proxy ${proxy} to communicate with the CrowdStrike Apis" - stdout $true
422
+ }
423
+
424
+ if ($proxy ) {
425
+ $WebRequestParams.Add (' Proxy' , $proxy )
426
+ }
427
+
428
+ $BaseUrl = Get-FalconCloud $FalconCloud
429
+
430
+ $Body = @ {}
431
+ $Body [' client_id' ] = $FalconClientId
432
+ $Body [' client_secret' ] = $FalconClientSecret
433
+
434
+ if ($MemberCid ) {
435
+ $Body [' member_cid' ] = $MemberCid
436
+ }
437
+
438
+ $BaseUrl , $Headers = Invoke-FalconAuth - WebRequestParams $WebRequestParams - BaseUrl $BaseUrl - Body $Body - FalconCloud $FalconCloud
439
+ $Headers [' Content-Type' ] = ' application/json'
440
+ $WebRequestParams.Add (' Headers' , $Headers )
441
+ }
442
+ elseif ($RemoveHost ) {
443
+ $Message = ' Unable to remove host without credentials, please provide FalconClientId and FalconClientSecret or FalconAccessToken'
444
+ throw $Message
445
+ }
446
+ elseif ($GetAccessToken ) {
447
+ $Message = ' Unable to get access token without credentials, please provide FalconClientId and FalconClientSecret'
448
+ throw $Message
380
449
}
381
450
382
451
$UninstallerPath = $null
@@ -387,7 +456,8 @@ process {
387
456
388
457
if (Test-Path - Path $UninstallerPathDir ) {
389
458
$UninstallerPath = Get-ChildItem - Path $UninstallerPathDir - Recurse | Where-Object { $_.Name -match $UninstallerName } | ForEach-Object { $_.FullName } | Sort-Object - Descending | Select-Object - First 1
390
- } else {
459
+ }
460
+ else {
391
461
$UninstallerPath = $null
392
462
}
393
463
}
@@ -403,16 +473,8 @@ process {
403
473
throw $Message
404
474
}
405
475
406
- # Verify creds are provided if using the API
407
- $credsProvided = Test-FalconCredential $FalconClientId $FalconClientSecret
408
- if (! $credsProvided -and ! $FalconAccessToken ) {
409
- if ($RemoveHost ) {
410
- $Message = ' Unable to remove host without credentials, please provide FalconClientId and FalconClientSecret or FalconAccessToken'
411
- throw $Message
412
- }
413
- }
414
- else {
415
- # Grab AID before uninstalling
476
+ # Grab AID before uninstalling. Only relevant if $RemoveHost or if $AuthProvided and !$MaintenanceToken
477
+ if ($RemoveHost -or ($AuthProvided -and ! $MaintenanceToken )) {
416
478
Write-FalconLog ' GetAID' ' Getting AID before uninstalling'
417
479
$aid = Get-AID
418
480
if (! $aid ) {
@@ -424,51 +486,6 @@ process {
424
486
Write-FalconLog ' GetAID' $Message
425
487
}
426
488
427
- # Hashtable for common Invoke-WebRequest parameters
428
- $WebRequestParams = @ {}
429
-
430
- # Configure proxy based on arguments
431
- $proxy = " "
432
- if ($ProxyHost ) {
433
- Write-Output " Proxy settings detected in arguments, using proxy settings to communicate with the CrowdStrike api"
434
-
435
- if ($ProxyHost ) {
436
- $proxy_host = $ProxyHost.Replace (" http://" , " " ).Replace(" https://" , " " )
437
- Write-FalconLog - Source " Proxy" - Message " Proxy host ${proxy_host} found in arguments" - stdout $true
438
- }
439
-
440
- if ($ProxyPort ) {
441
- Write-FalconLog - Source " Proxy" - Message " Proxy port ${ProxyPort} found in arguments" - stdout $true
442
- $proxy = " http://${proxy_host} :${ProxyPort} "
443
- }
444
- else {
445
- $proxy = " http://${proxy_host} "
446
- }
447
-
448
- $proxy = $proxy.Replace (" '" , " " ).Replace(" `" " , " " )
449
- Write-FalconLog - Source " Proxy" - Message " Using proxy ${proxy} to communicate with the CrowdStrike Apis" - stdout $true
450
- }
451
-
452
- if ($proxy ) {
453
- $WebRequestParams.Add (' Proxy' , $proxy )
454
- }
455
-
456
- if ($credsProvided -or $FalconAccessToken ) {
457
- $BaseUrl = Get-FalconCloud $FalconCloud
458
-
459
- $Body = @ {}
460
- $Body [' client_id' ] = $FalconClientId
461
- $Body [' client_secret' ] = $FalconClientSecret
462
-
463
- if ($MemberCid ) {
464
- $Body [' member_cid' ] = $MemberCid
465
- }
466
-
467
- $BaseUrl , $Headers = Invoke-FalconAuth - WebRequestParams $WebRequestParams - BaseUrl $BaseUrl - Body $Body - FalconCloud $FalconCloud
468
- $Headers [' Content-Type' ] = ' application/json'
469
- $WebRequestParams.Add (' Headers' , $Headers )
470
- }
471
-
472
489
if ($RemoveHost ) {
473
490
# Remove host from CrowdStrike Falcon
474
491
Write-FalconLog ' RemoveHost' ' Removing host from Falcon console'
0 commit comments