相当于CURL命令的Powershell

时间:2018-07-04 12:37:55

标签: powershell powershell-v3.0

CURL命令如下。

curl -ik -X PUT -H "X-API-Version: 600" -H "Auth:$AUTH" -H "Content-Type: multipart/form-data" -F "filename=@./temp.crl;type=application/pkix-crl" https://$HOST_IP/rest/$ALIASNAME/crl

我想使用Invoke-RestMethod获得等效的Powershell。我尝试了以下代码。

$url='https://'+$hostname+'/rest/+$aliasname+'/crl'
$url
$hdrs=@{}
$hdrs.Add('X-API-Version', '600');
$hdrs.Add('Auth', $Auth);
$hdrs.Add('Content-Type', 'application/json');
$path=get-location
$FileContent = [IO.File]::ReadAllBytes(temp.crl)
$file=@{'filename'=$FileContent}
Invoke-RestMethod -Uri $url -ContentType 'mutlipart/form-data' -Method Put -Headers $hdrs -Body $file

它返回400(错误请求错误)

2 个答案:

答案 0 :(得分:1)

使用-InFile参数,请尝试以下操作:

$FilePath = '/temp.crl' # Make sure the path is correct #
$Headers = @{"X-API-Version" = 600;Auth = $AUTH}
Invoke-RestMethod -Uri "https://$HOST_IP/rest/$ALIASNAME/crl" `
-ContentType 'multipart/form-data' `
-Headers $Headers -Method Put -InFile $FilePath

答案 1 :(得分:1)

此代码段应该解决您的问题。您的问题有多种错别字和逻辑冲突。

$restArgs = @{
    Uri         = "https://$hostname/rest/$aliasname/crl"
    Headers     = @{
        'X-API-Version' = 600
        Auth            = $Auth
    }
    Method      = 'Put'
    Body        = @{
        filename = [IO.File]::ReadAllBytes("$PSScriptRoot\temp.crl")
    }
    ContentType = 'multipart/form-data'
}
Invoke-RestMethod @restArgs