继续收到错误:“无法绑定参数'Headers'”

时间:2019-06-21 15:38:06

标签: powershell error-handling

每次我尝试将参数发送到Invoke-RestMethod时,我总是收到错误消息:

Cannot bind parameter 'Headers'. Cannot convert the "{
    "Content-Type":  "application/json",
    "Authorization":  "\"ZmY2ZTZiYjEtNmM5Mi00MzAyLWJjM2MtY2ZhYmFlOWVlNDE0OzIwMkI0MzdBNjExODQxQTA7MDAwMDAwMDJGRkNCRkJFREE1QzUyMUExNzFDNEJFMUNEMEYxMUM
2OTEzQTRERkMwNEExRkU3MjZBRDkyMDAxMjcwN0Q1RTQ0MDAwMDAwMDA7\""

}" value of type "System.String" to type "System.Collections.IDictionary"..
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Uploadv6.4.ps1

我已将变量转换为JSON格式,但现在我不知道该做些什么。

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]";

$seckey = MyPAMLogin $username $password;
$headers.Add("Authorization", $seckey);
$headers.Add("Content-Type", $contentType);
$seckey = $seckey | ConvertTo-Json
$headers = $headers | ConvertTo-Json;

$newAccount = [ordered]@{
    safe = $safeName;
    platformID = $platformID;
    username = $username;
    AccountName = $AccountName;
    password = "abc";
    secretType = "key";
    secret = "";
    platformAccountProperties = {};
    DisableAutoMgmt = $disableAutoMgmtReason;
    DisableAutoMgmtReason = $disableAutoMgmtReason;
}
$addAccount = $newAccount | ConvertTo-Json;

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$response = Invoke-RestMethod -Uri https://$Environment/PasswordVault/API  -Method Post -Headers $headers -Body $addAccount -ContentType 'application/json'

0 个答案:

没有答案