2

以下代码在 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 核心中运行时,我收到以下错误:

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

知道这里有什么问题吗?非常感谢提前。

4

2 回答 2

0

解决方案是安装 powershell-6.1.0-preview.4-osx-x64。我使用的是 powershell-6.0.3-osx-x64。

于 2018-08-10T09:43:05.730 回答
0

不幸的是,我自己无法对此进行测试,但经过一番观察,Core 中默认启用了 Strict Header 验证,并且影响了 Web 命令行开关。

尝试将交换机-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

于 2018-08-09T00:59:55.890 回答