0
votes

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

1

1 Answers

0
votes
  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