如何使用Invoke-RestMethod上传jpg

use*_*277 5 powershell

我正在尝试使用PowerShell命令行开关Invoke-RestMethod将图片发布到网址.这是我的命令:

$usercreds = Get-Credential
$pic = Get-Content \\server\share\pic.jpg
$uri = http://website/sub/destination

Invoke-RestMethod -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:"该文件不是有效的图像文件." 我也尝试使用Invoke-WebRequest,结果相同.Web服务器不是我们的,并且他们一边说技术人员说使用curl,但是我们不知道怎么也没有Linux盒子.有什么我想念的吗?我可以毫无问题地打开jpg,所以它没有腐败或任何东西.

我试过这个,但是服务器对我大吼:使用PowerShell Invoke-RestMethod来发布大型二进制多部分/表单数据

错误代码:

PS C:\Windows\system32> Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds
Invoke-WebRequest : {"error":{"message":"the file is not a valid image file"},"data":[],"meta":"error"}
At line:1 char:1
+ Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'imag ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)

TTo*_*oni 14

尝试使用-Infile参数.Get-Content将你的文件解释为一串字符串,只是搞砸了.

$usercreds = Get-Credential
$picPath = "\\server\share\pic.jpg"
$uri = http://website/sub/destination

Invoke-WebRequest -uri $uri -Method Put -Infile $picPath -ContentType 'image/jpg' -Credential $usercreds
Run Code Online (Sandbox Code Playgroud)