The CreatorCon Call for Content is officially open! Get started here.

Problems with Powershell Invoke-WebRequest

simonapa
Mega Contributor

Hello! We have a large number of Powershell scripts supporting our environment, I am working on a new functionality that integrates a more to ServiceNow but have hit a wall.

One script has a number of requirements that will require interaction with SN.

1. Updating/writing to an existing ticket.

2. Searching for a ticket.

3. Reading from a ticket.

From the server that executes the scripts, I am getting errors:

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

At line:23 char:13

+ $response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -proxy ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      + CategoryInfo                   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException

      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

<code>

# Eg. User name="admin", Password="admin" for this code sample.


$user = "admin"

$pass = "admin"

# added the following line to add path to proxy

$proxy = 'http://proxy.url:8080'


$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# this was added to assist with SSL Trust message

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))

$headers.Add('Accept','application/json')


$uri = https://instance.service-now.com/api/now/table/incident?sysparm_limit=1


$method = "get"


$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -proxy $proxy -ProxyUseDefaultCredentials


</code>

Where have I gone wrong? Any assistance would be appreciated.

1 ACCEPTED SOLUTION

simonapa
Mega Contributor

Hello everyone, I wanted to share the workaround to this issue.



By adding the following to the my script I am able to get full functionality to our instance.




<code>


      Add-Type @"
      using System;
      using System.Net;
      using System.Net.Security;
      using System.Security.Cryptography.X509Certificates;
      public class ServerCertificateValidationCallback
      {
              public static void Ignore()
              {
                      ServicePointManager.ServerCertificateValidationCallback +=
                              delegate
                              (
                                      Object obj,
                                      X509Certificate certificate,
                                      X509Chain chain,
                                      SslPolicyErrors errors
                              )
                              {
                                      return true;
                              };
              }
      }
"@

[ServerCertificateValidationCallback]::Ignore();
</code>



I hope this helps someone else out too.


View solution in original post

5 REPLIES 5

Hi simonapa

 

Can you show me where i put this example code in this exemple down ?  thanks 

 

</code>

 

$user = "admin"

$pass = "admin"

# added the following line to add path to proxy

$proxy = 'http://proxy.url:8080'


$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# this was added to assist with SSL Trust message

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))

$headers.Add('Accept','application/json')

 

$uri = https://instance.service-now.com/api/now/table/incident?sysparm_limit=1

 

$method = "get"


$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -proxy $proxy -ProxyUseDefaultCredentials


</code>