Skip to content

Commit

Permalink
Add support for PS 5.1 +semver: feature (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobFaie authored Aug 21, 2020
1 parent bf04587 commit d02a98a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
4 changes: 2 additions & 2 deletions PwshTenable.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ModuleVersion = '0.2.1'

# Supported PSEditions
CompatiblePSEditions = @('Core')
CompatiblePSEditions = @( 'Core', 'Desktop' )

# ID used to uniquely identify this module
GUID = '8c812728-9bcf-4376-ae70-22cda4dfe5fa'
Expand All @@ -33,7 +33,7 @@
# Description = ''

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '6.0'
PowerShellVersion = '5.1'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ stages:

- template: test-stage.yml
parameters:
PS51: false
PS51: true

- stage: Deploy
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
Expand Down
58 changes: 36 additions & 22 deletions functions/Invoke-Method.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function Invoke-Method {

$params = @{
Method = $Method
SslProtocol = 'Tls12'
Verbose = $VerbosePreference
Headers = @{
'Accept' = 'application/json'
Expand All @@ -88,37 +87,52 @@ function Invoke-Method {

$uri = 'https://cloud.tenable.com/' + $Path.TrimStart('/')

[Net.ServicePointManager]::SecurityProtocol += [System.Net.SecurityProtocolType]::Tls12

$multiplier = 0.5
while (-not [String]::IsNullOrEmpty($uri)) {
try {
$result = Invoke-RestMethod @params -Uri $uri
$result
$uri = $null

} catch [Microsoft.PowerShell.Commands.HttpResponseException] {
} catch {
$errorRecord = $_
$response = $errorRecord.Exception.Response

if ($Retry -and $response.StatusCode -in @(429, 502, 503, 504)) {
$response | Select-Object StatusCode, ReasonPhrase | ConvertTo-Json -Compress | Write-Warning

$retryAfter = $response.Headers.RetryAfter ? $response.Headers.RetryAfter.Delta.TotalSeconds : 5

$multiplier *= 2
$jitter = Get-Random -Minimum 0.0 -Maximum 1.0
$sleep = $retryAfter + $multiplier + $jitter

if ($sleep -gt 30) {
Write-Warning -Message "Long sleep! $sleep"
if ($errorRecord.Exception.GetType().Name -in @('HttpResponseException', 'WebException')) {
$response = $errorRecord.Exception.Response

if ($Retry -and $response -and $response.StatusCode -in @(429, 502, 503, 504)) {
$response | Select-Object StatusCode, ReasonPhrase | ConvertTo-Json -Compress | Write-Warning

$retryAfter = 5
if ($errorRecord.Exception -is [System.Net.WebException]) {
if ($response.Headers['Retry-After']) {
$retryAfter = $response.Headers['Retry-After']
}
} else {
if ($response.Headers.RetryAfter) {
$retryAfter = $response.Headers.RetryAfter.Delta.TotalSeconds
}
}

$multiplier *= 2
$jitter = Get-Random -Minimum 0.0 -Maximum 1.0
$sleep = $retryAfter + $multiplier + $jitter

if ($sleep -gt 30) {
Write-Warning -Message "Long sleep! $sleep"
}
$null = Start-Sleep -Seconds $sleep

} else {
$errorMessage = $errorRecord.ToString()
Get-PSCallStack | ForEach-Object { $errorMessage += "`n" + $_.Command + ': line ' + $_.ScriptLineNumber }
throw $errorMessage
}
$null = Start-Sleep -Seconds $sleep

} else {
throw $errorRecord
$errorMessage = $errorRecord.ToString()
Get-PSCallStack | ForEach-Object { $errorMessage += "`n" + $_.Command + ': line ' + $_.ScriptLineNumber }
throw $errorMessage
}
} catch {
$errorRecord = $_
throw $errorRecord
}
}
}

0 comments on commit d02a98a

Please sign in to comment.