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

gauravchoudhury
Tera Guru

Hi Simon,



If your computer is joined to a domain, then Invoke-Request from the computer from the same domain without Credential parameter.


Thanks Gaurav,



I am executing from the domain controller in this instance. The -Credential parameter is required to authenticate to ServiceNow DEV instance ( with a different account )



I have confirmed functionality of the script outside of our environment, so I am looking for some further guidance on troubleshooting in a secured environment.



Are there additional logs or debug verbose I can enable?


Hi Simon,



One feature PowerShell provides as a debugging feature is breakpoints, which have been around for a very long time and are not new to PowerShell. In simplest terms, a breakpoint is a spot in your script that, when executed, will break (pause) the script's execution. When a PowerShell script hits a breakpoint, it will pause execution and wait for input.



  • Open the script (myscript.ps1) in Windows Powershell ISE
  • Press F9 at the variable you want to inspect (debug).
  • In the shell window provide the relative path of the script along with the param value. (For instance: .\myscript.ps1 "my value")
  • Hit enter (you don't need to hit F5)
  • You'll be able to see the debugging breakpoints in highlighted with yellow color. Place your cursor to the desired variable to inspect the current value.

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.