以下代码在Windows Server上的PowerShell中完美运行
$user = "XXXXX"
$pass = "XXXXX"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
#$FilePath = 'C:\Upload\User Entitlement Export.csv';
$FilePath = '/Users/XXXXXX/Downloads/User Entitlement Export.csv';
$URL = 'https://XXXXXXXXXXX.com/sys_import.do?sysparm_transform_after_load=true&sysparm_import_set_tablename=u_alm_entitlement_user';
$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString();
#$boundary = 'another cool boundary';
$LF = "`r`n";
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"User Entitlement Export.csv`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
) -join $LF
Write-Host $bodyLines;
Invoke-RestMethod -Proxy http://XXXXXXX:8080 -ProxyUseDefaultCredentials -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines -Headers $headers
但是当我在Mac上的PowerShell Core中运行时,会收到以下错误:
Invoke-RestMethod : The format of value 'multipart/form-data;charset=utf-8;boundary="d0d0dccd-698c-48b8-8770-6bcc7d29f609"' is invalid.
At /Users/laurensbrand/Downloads/uploadCSV.ps1:33 char:1
+ Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], FormatException
+ FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
有什么想法吗?非常感谢。
答案 0 :(得分:0)
不幸的是,我无法自己对此进行测试,但稍作观察后,似乎Core中默认启用了严格标头验证,并且影响了Web Commandlet。
尝试将开关-SkipHeaderValidation
与Core版本一起使用。
更多信息在这里:
https://github.com/PowerShell/PowerShell/issues/6002#issuecomment-359987432
https://get-powershellblog.blogspot.com/2017/11/powershell-core-web-cmdlets-in-depth.html#L08
答案 1 :(得分:0)
解决方案是安装powershell-6.1.0-preview.4-osx-x64。我使用的是powershell-6.0.3-osx-x64。