我有以下抛出异常的PowerShell脚本,如下所示。如何Invoke-RestMethod -Method Post
授权$bearerHeader
?
$content = New-Object System.Net.Http.StreamContent $snapshot;
$content.CopyToAsync($snapshot);
try {
$encodedPath = [Text.Encoding]::ASCII.GetBytes($this.snapshotPath)
$hmacsha = New-Object System.Security.Cryptography.HMACSHA512
$hmacsha.key = [Convert]::FromBase64String($this.apikey);
$bearerToken = $hmacsha.ComputeHash($encodedPath)
$bearerToken = [Convert]::ToBase64String($bearerToken)
$bearerToken = $bearerToken.Split('=')[0]
$bearerToken = $bearerToken.Replace('*', '-')
$bearerToken = $bearerToken.Replace('+', '_')
$bearerHeader = @{ "Authorization" = ("Bearer", $bearerToken -join " ") }
$url = ($this.baseAddress, $this.snapshotPath -join "");
$resp = Invoke-RestMethod -Method Post -Uri $this.snapshotPath -ContentType "application/octet-stream" -Body $content -Headers $bearerToken
} catch [Exception] {
Write-Host "Exception: "$_.Exception.Message -ForegroundColor Red;
}
无法将“System.String”类型的“uZZETvpJdAIqIH_235oFjan3wS_M582332jmiJjm23jJvJxJwHm0JtWSkhg5qj_7jGDI_s3IFx2ozx5wyQXCA”值转换为“System.Collections.IDictionary”类型。
答案 0 :(得分:1)
根据评论,这个:
$resp = Invoke-RestMethod -Method Post -Uri $this.snapshotPath -ContentType "application/octet-stream" -Body $content -Headers $bearerToken
需要:
$resp = Invoke-RestMethod -Method Post -Uri $this.snapshotPath -ContentType "application/octet-stream" -Body $content -Headers $bearerHeader
-Headers参数需要一个哈希表作为其输入,您的代码将其创建为$ bearerHeader但是您没有使用它。