首页 > 解决方案 > 尝试使用 power shell 将 zip 文件上传到 nexus,但失败了

问题描述

我对 power shell 脚本非常陌生,在将 zip 文件上传到 nexus 时遇到问题,下面是我用于上传的 power shell 脚本

$FilePath = "C:\Users\asd\SomeFile.zip";
$Uri = 'https://some.place.over.the.rainbow.com/api/v1/dataupdate';
$user = 'user_name'
$pass = 'abc123'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
    Authorization = $basicAuthValue
};

$fileBin = [IO.File]::ReadAllBytes($FilePath)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileEnc = $enc.GetString($fileBin)
$boundary = [System.Guid]::NewGuid().ToString()   
$LF = "`r`n"; # and tried with $LF = "`n";
$bodyLines = (
        "--$boundary",
        "Content-Disposition: form-data; name=`"file`"$LF",   
        $fileEnc,
        "--$boundary--$LF"
        ) -join $LF

Invoke-RestMethod -Uri $Uri -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Headers $Headers -Body $bodyLines 

它失败并出现以下错误

cannot send content-body with this verb-type

我正在使用 powershell 5.x 版

标签: powershellpowershell-2.0invoke-command

解决方案


推荐阅读