执行 powershell 脚本时出错

Posted

技术标签:

【中文标题】执行 powershell 脚本时出错【英文标题】:Error while executing a power shell script 【发布时间】:2019-08-15 20:48:30 【问题描述】:

我正在尝试从 power shell 创建一个 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”。指定以下枚举值之一,然后重试。可能的枚举值为 “系统默认,Ssl3,Tls,Tls11,Tls12”。 在 C:\Users\njujjavarapu\Desktop\Snapshot.ps1:7 char:1 + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolTy ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], 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】:
    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"

    我收到以下错误

    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 问题,请尝试以下解决方法:Powershell v3 Invoke-WebRequest HTTPS error

替换

[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

【讨论】:

您好,在编辑之后,我收到了一个新错误。 Invoke-WebRequest :底层连接已关闭:发送时发生意外错误。在 C:\Users\njujjavarapu\Desktop\Snapshot.ps1:8 char:1 + Invoke-WebRequest -Uri njidlsdsapp01/support + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand @jujjavarapunagavenkatesh 很难说到底是什么原因造成的。尝试在invoke-RestMethod 之后添加$Error[0].Exception.InnerException 以查看确切的错误。 Hello @beatcracker 在添加该行后,我收到以下错误。有什么建议吗?? 此线程中没有可用于运行脚本的运行空间。您可以在 System.Management.Automation.Runspaces.Runspace 类型的 DefaultRunspace 属性中提供一个。您尝试调用的脚本块是:$true @jujjavarapunagavenkatesh 这是一个已知问题,我已经用解决方法更新了我的答案。

以上是关于执行 powershell 脚本时出错的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell ISE:调试从 power-shell 脚本调用的另一个 power-shell 脚本

“创建管道时出错。”在 Azure 管道自托管代理中运行 powershell 脚本时

win10 powershell禁止运行脚本解决

SQL Server自动化运维系列——批量执行SQL脚本(Power Shell)

执行 Exchange PS 远程处理时出错:方法调用失败?

在带有任务计划程序信息的提示符下运行 powershell 时出错 - “不被识别为 cmdlet、函数、脚本文件或可操作的名称..”