1

I'm trying to create a Rest API's from a power shell, when i'm trying to run the script i'm getting the following error. I wasn't sure what i was making a mistake.

I'll put the script and the error also. Please help.

Script:
username = "admin"
password = "******"
authInfo = ("{0}:{1}" -f $username,$password)
authInfo = [System.text.Encoding]:: UTF8.GetByteCount($authInfo)
authInfo = [System.Convert]::ToBase64String($authInfo)
headers = @{Accept=("application/json");Contenttype=("appliaction/json");Authorization=("Basic {0}"-f $authInfo)}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::"tls12, tls11, tls"
Invoke-WebRequest -Uri https://njidlsdsapp01/support

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

uri = "https://njidlsdsapp01/support"
body = "{'login':'admin','login_pwd':'*****','commands' :['create a new support file']}" 

headers

res = invoke-RestMethod -Uri $uri -Headers $headers -Method Post -Body $body
res

Error: PS C:\Users\njujjavarapu> C:\Users\njujjavarapu\Desktop\Snapshot.ps1 Exception setting "SecurityProtocol": "Cannot convert null to type "System.Net.SecurityProtocolType" due to enumeration values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are "SystemDefault,Ssl3,Tls,Tls11,Tls12"." At C:\Users\njujjavarapu\Desktop\Snapshot.ps1:7 char:1 + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolTy ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullyQualifiedErrorId : ExceptionWhenSetting

Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel. At C:\Users\njujjavarapu\Desktop\Snapshot.ps1:8 char:1 + Invoke-WebRequest -Uri https://njidlsdsapp01/support + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Contenttype                    appliaction/json                                                                                                                                                                                                                  
Accept                         application/json                                                                                                                                                                                                                  
Authorization                  Basic Eg==                                                                                                                                                                                                                        

invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel. At C:\Users\njujjavarapu\Desktop\Snapshot.ps1:19 char:8 + $res = invoke-RestMethod -Uri $uri -Headers $headers -Method Post -Bo ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

1 Answers1

0
  1. Exception setting "SecurityProtocol":
    "Cannot convert null to type "System.Net.SecurityProtocolType"
    due to enumeration values that are not valid.
    Specify one of the following enumeration values and try again.
    The possible enumeration values are "SystemDefault,Ssl3,Tls,Tls11,Tls12"."
    

Use

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]"tls12, tls11, tls"

or just

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
  1. I got the following error

    There is no Runspace available to run scripts in this thread.
    
    You can provide one in the DefaultRunspace property
    of the System.Management.Automation.Runspaces.Runspace type.
    
    The script block you attempted to invoke was: $true
    

This is a know PowerShell issue, try this workaround: Powershell v3 Invoke-WebRequest HTTPS error

Replace

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

with

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • Hello, after that edit, i got a new error. Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. At C:\Users\njujjavarapu\Desktop\Snapshot.ps1:8 char:1 + Invoke-WebRequest -Uri https://njidlsdsapp01/support + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand – jujjavarapu naga venkatesh Mar 25 '19 at 16:24
  • @jujjavarapunagavenkatesh It's hard to say what's actually causing this. Try adding `$Error[0].Exception.InnerException` after `invoke-RestMethod` to see the exact error. – beatcracker Mar 25 '19 at 16:47
  • Hello @beatcracker i got the following error, after adding that line. any suggestions?? – jujjavarapu naga venkatesh Mar 26 '19 at 14:33
  • There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: $true – jujjavarapu naga venkatesh Mar 26 '19 at 14:39
  • @jujjavarapunagavenkatesh This a known issue, I've updated my answer with workaround. – beatcracker Mar 26 '19 at 15:01
  • Any other suggestions please? – jujjavarapu naga venkatesh Mar 28 '19 at 14:32