Bre*_*t G 6 powershell powershell-v3.0 powershell-v4.0
我有一个 Powershell 脚本,它使用了在 powershell 3.0 中工作的 Invoke-RestMethod。但是,我升级到 powershell 4.0 以修复 powershell 3 中的错误。当我这样做时,我的脚本似乎已停止工作。
$username = "Administrator"
$password = "PASSWORD"
$uri = "https://10.0.0.18/vmrest/users"
$dictionary = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f$username,$password)))
$dictionary.Add("Authorization",$base64AuthInfo)
Invoke-RestMethod -Uri $uri -Method GET -Headers $dictionary -Verbose
当我打开详细开关时,它给了我这个响应
VERBOSE: GET https://192.168.1.18/vmrest/users with 0-byte payload
VERBOSE: received -1-byte response of content type
我也尝试指定请求的内容类型,但没有骰子
$dictionary.Add("Accept","application/json")
$dictionary.Add("Connection", "keep_alive")
让我印象深刻的一件事是,由于您使用的是 HTTPS,我确定您一定会收到证书错误,因为您的 URL 是 IP 地址。
您需要告诉 Powershell(实际上是 .NET 框架)忽略证书错误。否则它会在诸如 Invoke-WebRequest 之类的事情上失败。
尝试这个:
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Run Code Online (Sandbox Code Playgroud)
这是一个自定义证书验证回调,它总是返回 true 从而有效地忽略证书问题。
| 归档时间: |
|
| 查看次数: |
7851 次 |
| 最近记录: |