我的PowerShell配置文件中有两个脚本(CurrentUser.AllHosts),这些脚本让我头疼,因为它们在看似相同的控制台上给出了不同的结果。 在Powershell(提升)上,脚本没有任何问题,但在ConEmu(提升)和Powershell ISE中失败并出现Invoke-RestMethod错误 - 请参阅下面的完整错误。
脚本是
#Checking latest version of Git
function Show-GitCurrentRelease() {
[cmdletbinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$uri = "https://api.github.com/repos/git-for-windows/git/releases/latest"
)
Begin {
Write-Verbose "[BEGIN] Starting: $($MyInvocation.MyCommand)"
}
Process {
Write-Verbose "[PROCESS] Getting current release information from $uri"
$data = Invoke-RestMethod -Uri $uri -Method Get
if ($data.tag_name) {
[PSCustomObject]@{
Name = $data.name
Version = $data.tag_name
Released = $($data.published_at -as [datetime])
}
}
}
End {
Write-Verbose "[END] Ending: $($MyInvocation.MyCommand)"
}
} # Close Show-GitCurrentRelease
和
# Downloading latest version of Git
function Get-GitCurrentRelease() {
# Download the latest 64bit version of Git for Windows
$uri = 'https://git-scm.com/download/win'
# Path to store the downloaded file
# In this case it's saved to the temp directory for the session
$path = $env:TEMP
# Get the web page
$page = Invoke-WebRequest -Uri $uri -UseBasicParsing
# Get the download link
$dl = ($page.links | where outerhtml -Match 'git-.*64-bit.exe' | select -First 1 * ).href
# Split out the file name
$filename = Split-Path $dl -Leaf
# Construct a file path for the download
$out = Join-Path -Path $path -ChildPath $filename
# Download the file
Invoke-WebRequest -Uri $dl -OutFile $out -UseBasicParsing
# Check it out
Get-Item $out
} # Close Get-GitCurrentRelease
在Powershell中运行Show-GitCurrentRelease
(提升控制台)我得到以下结果:
Name Version Released
---- ------- --------
Git for Windows 2.16.2 v2.16.2.windows.1 2018-02-20 13:32:41
但是如果我在配置为运行提升的Powershell提示符的ConEmu中运行相同的命令,我会得到以下内容:
Invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel.
At [HOME]\Documents\WindowsPowerShell\profile.ps1:63 char:17
+ $data = Invoke-RestMethod -Uri $uri -Method Get
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我与Get-GitCurrentRelease
得到了类似的结果。在PS上它将最新的Git-installer下载到TMP文件夹,同时我在ConEmu中收到Invoke-RestMethod错误。
我一直在谷歌搜索错误,据我所知,这个错误的通常原因是PowerShell和脚本访问的站点之间的TLS版本不匹配。但是在这种情况下,这似乎是另一回事,因为它直接在PS控制台中运行,但在ConEmu中却没有,这只是控制台周围的包装/皮肤 - 在这种情况下(提升)PS。
Powershell ISE给我带来了相同的Invoke-RestMethod错误。
我测试了在ConEmu,PS和PS ISE上运行$ Host并得到以下结果:
ConEmu中$ host的结果(提升):
Name : ConsoleHost
Version : 5.1.16299.248
InstanceId : fb8e89c9-7395-47ab-b732-a102da041999
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : sv-SE
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
在PowerShell中(提升):
Name : ConsoleHost
Version : 5.1.16299.248
InstanceId : ec79dc29-4444-4230-a0ee-a2ead575903f
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : sv-SE
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
最后在PowerShell ISE(提升)中:
Name : Windows PowerShell ISE Host
Version : 5.1.16299.248
InstanceId : 7180cdbf-50ee-4187-8fc0-ac0b230095fa
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : sv-SE
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
除了(预期的)InstanceId和PS ISE具有不同的PrivateData值(非意外)之外,ConEmu和PS是相同的。
如果我能看到不同控制台之间存在任何有意义的差异,那么
会更容易答案 0 :(得分:0)
错误:
Invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel
正在发生,因为默认情况下,PowerShell在您请求的站点使用更高版本时使用TSL 1.0。
要将脚本设置为使用更高版本,请在调用Invoke-RestMethod:
之前尝试此操作 $AllProtocols = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
[Net.ServicePointManager]::SecurityProtocol = $AllProtocols