From d74a2e5c177381a355abecf5f7429c513c2bdeb1 Mon Sep 17 00:00:00 2001 From: Greg Brownstein Date: Tue, 7 Nov 2023 20:20:05 -0500 Subject: [PATCH 1/2] add timeout to api calls --- ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 | 6 ------ ServiceNow/Public/New-ServiceNowSession.ps1 | 11 +++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 b/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 index 1f8655e..5eba518 100644 --- a/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 +++ b/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 @@ -138,12 +138,6 @@ function Invoke-ServiceNowRestMethod { $params.Body = [System.Text.Encoding]::UTf8.GetBytes($Body) } - # if ( $Body ) { - # $params.Body = $Body - # } - - # Write-Verbose ($params | ConvertTo-Json) - # hide invoke-webrequest progress $oldProgressPreference = $ProgressPreference $ProgressPreference = 'SilentlyContinue' diff --git a/ServiceNow/Public/New-ServiceNowSession.ps1 b/ServiceNow/Public/New-ServiceNowSession.ps1 index 002576a..9ea2e78 100644 --- a/ServiceNow/Public/New-ServiceNowSession.ps1 +++ b/ServiceNow/Public/New-ServiceNowSession.ps1 @@ -35,6 +35,10 @@ Use GraphQL instead of REST calls .PARAMETER GetAllTable Populate $ServiceNowTable with data from all tables the user has access to +.PARAMETER TimeoutSec +Timeout in seconds for all operations. +The default is 0 which represents no timeout. + .PARAMETER PassThru Provide the resulting session object to the pipeline as opposed to setting as a script scoped variable to be used by default for other calls. This is useful if you want to have multiple sessions with different api versions, credentials, etc. @@ -119,6 +123,10 @@ function New-ServiceNowSession { [Parameter()] [switch] $GraphQL, + [Parameter()] + [ValidateRange(0, [int32]::MaxValue)] + [int32] $TimeoutSec = 0, + [Parameter()] [switch] $PassThru ) @@ -148,6 +156,9 @@ function New-ServiceNowSession { } } + $PSDefaultParameterValues['Invoke-WebRequest:TimeoutSec'] = $TimeoutSec + $PSDefaultParameterValues['Invoke-RestMethodt:TimeoutSec'] = $TimeoutSec + switch -Wildcard ($PSCmdLet.ParameterSetName) { 'OAuth*' { $params = @{ From 740b4340e8345ba17e1f254cf72ad83e9419e5f5 Mon Sep 17 00:00:00 2001 From: Greg Brownstein Date: Tue, 7 Nov 2023 20:24:50 -0500 Subject: [PATCH 2/2] force script scope --- ServiceNow/Public/New-ServiceNowSession.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ServiceNow/Public/New-ServiceNowSession.ps1 b/ServiceNow/Public/New-ServiceNowSession.ps1 index 9ea2e78..20f5713 100644 --- a/ServiceNow/Public/New-ServiceNowSession.ps1 +++ b/ServiceNow/Public/New-ServiceNowSession.ps1 @@ -156,8 +156,8 @@ function New-ServiceNowSession { } } - $PSDefaultParameterValues['Invoke-WebRequest:TimeoutSec'] = $TimeoutSec - $PSDefaultParameterValues['Invoke-RestMethodt:TimeoutSec'] = $TimeoutSec + $script:PSDefaultParameterValues['Invoke-WebRequest:TimeoutSec'] = $TimeoutSec + $script:PSDefaultParameterValues['Invoke-RestMethodt:TimeoutSec'] = $TimeoutSec switch -Wildcard ($PSCmdLet.ParameterSetName) { 'OAuth*' {