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

查看:983
本文介绍了通过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:\Folder\file.jpg.我使用用于登录网站的凭据,可以在其中通过GUI上传这些图片.

I have checked $uploadPath and it is correct C:\Folder\file.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 Web网关,但是我正在与绕过它的用户一起运行脚本,因此不会造成这种情况.

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!

P.S.我也尝试过- Powershell脚本将日志文件从本地系统上传到http URL ,并返回Exception calling "UploadFile" with "3" argument(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身份验证作为当前登录的用户...

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天全站免登陆