$psnFQDN = $args[0] # PSN fqdn $userName = $args[1] # user name $sessionId = $args[2] # session id $script:postureJob = $args[3] # posture job name to be cleaned up $EPLogFile = $args[4] # Endpoint log file $CertThumbprints = $args[5] # Certs Thumbprints location $certPath = $args[6] # Cert location $script:retryNum = -1 $script:retryDelay = -1 $agent_dir = "agentless_" + "$sessionIdStrip" Remove-Item -Recurse -Force $agent_dir 2> $null New-Item $agent_dir -ItemType "directory" -Force cd "$agent_dir" $ExistingPluginHashU = $ExistingPluginHash.ToUpper() $downloadLinkAH = "https`://$psnFQDN`:8905/auth/provisioning/agentless?sessionId=$sessionId&agentVersion=$AgentVersion&hash=$ExistingPluginHashU" $script:PostureTimeout = 300 #go with initial timeout of 5 mins and reset this as soon as we get header value from posture $script:HeaderFile = $Admin_Dir + $agent_dir + '\header_' + $sessionIdStrip + '.txt' $script:PostureTimeoutSet = $false do { curl.exe -V 2> $null if ( $? ) { EP-Log-Message "Hitting URL $downloadLinkAH with Timeout $script:PostureTimeout" curl.exe -A "$osID.$platformId.$platformType.$majorVer.$minorVer.$productCode" $downloadLinkAH -o "$sessionIdStrip.zip" --cacert "$certPath" -D "header_$sessionIdStrip.txt" -m $script:PostureTimeout 2>&1 >> $EPLogFile if($LASTEXITCODE -eq 0){ EP-Log-Message "Curl command succeeded" } else { if ($LASTEXITCODE -eq 28) { Trigger-Status 1 "Curl Timeout Error Unable to download agentless posture" } else { Trigger-Status 1 "Curl Error code $LASTEXITCODE Unable to download agentless posture with return code" } $script:CurlFailure = $true break } } if(Test-Path -Path "header_$sessionIdStrip.txt"){ $script:HttpStatus = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "HTTP/1.1") -split " ")[1] $script:FileHash = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-FileHash") -split ": ")[1] $script:CachePlugin = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-CachingPlugin") -split ": ")[1] $script:DummyFile = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-DummyFile") -split ": ")[1] $script:DebugMode = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-DebugLogMode") -split ": ")[1] $script:StatusDelay = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-StatusDelay") -split ": ")[1] $script:LogRetentionDays = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-LogRetentionDays") -split ": ")[1] $script:DownloadLogDays = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-DownloadLogDays") -split ": ")[1] $script:NotificationPopupTime = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-NotificationPopupTime") -split ": ")[1] } else { Trigger-Status 1 "Unable to extract Headers from curl command while downloading agentless package" $script:CurlFailure = $true break } if(!$script:PostureTimeoutSet){ if(($HttpStatus -eq 200 ) -or ($HttpStatus -eq 429)){ $script:PostureTimeout = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-TimeOut") -split ": ")[1] $script:PostureTimeoutSet = $true ## Start the timer $script:timer = [Diagnostics.Stopwatch]::StartNew() } } } else { $StatusLink = "https`://$psnFQDN`:8905/auth/agentless/status?sessionId=" + $sessionId $Data = '{"status":"1","reason":"CURL is not installed on endpoint"}' EP-Log-Message "Reporting Curl absence via Invoke-WebRequest ... " Invoke-WebRequest -Uri $StatusLink -Method 'POST' -Headers @{'Accept'= 'application/json'; 'Content-Type'= 'application/json'} -Body $Data -Certificate $certPath } if ($HttpStatus -eq 200) { if (($DebugMode -ne 1) -and ($DebugMode -ne 3)){ Extract-LaunchAgentLessPLugin } break } elseif ($HttpStatus -eq 429) { if ($script:retryNum -eq -1) { $script:retryNum = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-RetryNum") -split ": ")[1] }else { $script:retryNum = $script:retryNum - 1 } if ($script:retryDelay -eq -1) { $script:retryDelay = ((Select-String -Path "header_$sessionIdStrip.txt" -Pattern "X-RetryDelay") -split ": ")[1] } } elseif (($DebugMode -ne 1) -and ($DebugMode -ne 3)) { Trigger-Status 1 "Error occurred unable to download agentless plugin" break } if (($script:retryDelay -gt 0) -and ($script:retryNum -gt 0)) { EP-Log-Message "waiting for $script:retryDelay secs before retrying download , retryNum $script:retryNum" Start-Sleep $script:retryDelay } } while ($script:retryNum -gt 0) if ($script:retryNum -eq 0) { Trigger-Status 1 "Retries to download via curl are done" }