执行Power Shell脚本时出错

时间:2019-03-25 15:01:23

标签: powershell

我试图从电源外壳创建Rest API,当我尝试运行脚本时遇到以下错误。我不确定自己在做什么错。

我还要放置脚本和错误。请帮忙。

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']}" 

标题

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

错误:   PS C:\ Users \ Njujjavarapu> C:\ Users \ njujjavarapu \ Desktop \ Snapshot.ps1       异常设置“ SecurityProtocol”:由于枚举值无效,无法将null转换为类型“ System.Net.SecurityProtocolType”。请指定以下枚举值之一,然后重试。可能的枚举值为       “” SystemDefault,Ssl3,Tls,Tls11,Tls12“。”       在C:\ Users \ njujjavarapu \ Desktop \ Snapshot.ps1:7 char:1       + [Net.ServicePointManager] :: SecurityProtocol = [Net.SecurityProtocolTy ...       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~           + CategoryInfo:未指定:(:) [],SetValueInvocationException           + FullyQualifiedErrorId:ExceptionWhenSetting

     

Invoke-WebRequest:请求被中止:无法创建SSL / TLS安全通道。       在C:\ Users \ njujjavarapu \ Desktop \ Snapshot.ps1:8 char:1       +调用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:请求已中止:无法创建SSL / TLS安全通道。       在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 个答案:

答案 0 :(得分: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"."
    

使用

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

或者只是

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

    我遇到以下错误

         
    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
    

这是一个已知的PowerShell问题,请尝试以下解决方法:https://github.com/netlify-templates/gatsby-starter-netlify-cms/blob/master/gatsby-node.js#L9

替换

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

使用

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