通过 Powershell 将文件上传到 HTTP [英] Uploading file to HTTP via Powershell

查看:39
本文介绍了通过 Powershell 将文件上传到 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个上传图片到服务器的 php 站点,简单的 jpg 文件,命名正确.问题是我们有时需要一次上传几百个,但 php 一次只接受 1 个,重命名它们并上传.PS中的文件操作做得很好,但是上传失败.

We have a php site that uploads pictures to server, simple jpg file with correct naming. Problem is that we need to upload sometimes couple of hundred of them at a time, but php accepts only 1 at a time, renames them and uploads. I have done file operations in PS quite well, but fail to upload.

与上传相关的 PHP 部分(据我所知)如下所示:<form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">

PHP part related to upload (as far as I can tell) looks like this: <form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">

我也在这里查看了 Google 和相关主题,然后找到了:

I checked Google, related topics here as well, and got to this:

$uri = "http://example.com/"
$pwd = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('myuser', $pwd)
$contentType = "multipart/form-data"
    $body = @{
        "FileName" = Get-Content($uploadPath) -Raw
    }
    Invoke-WebRequest -Uri $uri -Method Post -ContentType $contentType -Body $body

我已经检查了 $uploadPath 并且它是正确的 C:Folderfile.jpg.我使用凭据登录我可以通过 GUI 上传这些图片的站点.

I have checked $uploadPath and it is correct C:Folderfile.jpg. I use credentials that I use to log in to site where I can upload these pictures via GUI.

我试过在 POST 和 PUT 之间切换,没有任何变化.

I have tried switching between POST and PUT, with no changes.

http://example.com/file.jpg 替换 http://example.com 也没有区别.不确定,这是使用 POST 的正确方法.

Replacing http://example.com with http://example.com/file.jpg also provided no difference. Unsure, which is correct way to use POST.

我们公司有 McAffe 网络网关,但我正在使用绕过它的用户运行脚本,所以它不会导致这种情况.

We have McAffe web gateway in company, but I'm running script with user that bypasses it, so it's not causing this.

我收到的当前错误消息是:Invoke-WebRequest:基础连接已关闭:接收时发生意外错误."

Current error message that I'm getting is: "Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a receive."

任何帮助将不胜感激!对不起,如果这已经解决了,我只是错过了一个条目!

Any help would be greatly appreciated! And sorry if this has been already solved and I've simply missed an entry!

附言我也试过这个 - Powershell 脚本将日志文件从本地系统上传到 http URL,并返回 Exception call "UploadFile" with "3" arguments(s): "An exception occurred during a WebClient request."

P.S. I have also tried this - Powershell script to Upload log file from local system to http URL, and it returns Exception calling "UploadFile" with "3" argument(s): "An exception occurred during a WebClient request."

推荐答案

晚了一年,所以你可能不再需要答案了,但对我有用的是:

A year late, so you probably don't need the answer anymore, but what worked for me was:

Invoke-RestMethod -Uri $uri -Method Post -InFile $uploadPath -UseDefaultCredentials

我需要使用 windows auth 作为当前登录的用户... 从另一个答案 看来,您可以在某些情况下使用 -cred,而在其他情况下必须构建 Authorization 标头.

Mine needed to use windows auth as the currently logged in user... From this other answer, looks like you can use -cred in some cases and have to build out an Authorization header in others.

这篇关于通过 Powershell 将文件上传到 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆