【问题标题】:Multipart form script works perfectly in PowerShell but not in PowerShell core多部分表单脚本在 PowerShell 中完美运行,但在 PowerShell 核心中无法运行
【发布时间】:2018-08-08 06:25:29
【问题描述】:

以下代码在 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

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

【问题讨论】:

    标签: powershell-core


    【解决方案1】:

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

    尝试在核心版本中使用开关-SkipHeaderValidation

    更多信息在这里:

    https://github.com/PowerShell/PowerShell/issues/6002#issuecomment-359987432
    https://get-powershellblog.blogspot.com/2017/11/powershell-core-web-cmdlets-in-depth.html#L08

    【讨论】:

    • 当我自己用谷歌搜索这篇文章时,我显然没有正确阅读这篇文章。谢谢你指出。不幸的是, -SkipHeaderValidation 似乎存在问题,要么根本不起作用,要么在每种情况下都不起作用。必须继续努力:-)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多