How to fill php form type:multipart/form-data with multiple files in powershell

时间:2019-01-07 13:13:50

标签: php forms powershell multipartform-data

I cannot POST a php form with two files in powershell

I search the equivalent of the bash command : /usr/bin/curl -F fichier1=@/path_to_file1 -F fichier2=@/path_to_file2\MyUrl.com

The Php form "formulaire" wait two parameters : "fichier1" type file and "fichier2" type file

I've tried to follow the resolution of What is the right way to POST multipart/form-data using curl? but i have the error message A parameter cannot be found that matches parameter name 'F' I've tried to use the Invoke-Method with -Infile parameter but it accepts only one string parameter and I can't pass my two files path Hère is my code :

$Uri = "//MyUrl.com/backups/uploadLogsVms.php"
$filePath="C:\backup_points.csv"
$filePath2="C:\PROTECTED_VMS_JOB_SCHEDULE_2018_12_10.csv"
curl -v -F fichier1=@"$filePath" -F fichier2=@"$filePath2"

I expected to fill the form below and press the button boutEnvoyer with a powershell script:

<body>
<form name='formulaire' id='formulaire' method='post' action='/backups/uploadLogsVms.php' enctype='multipart/form-data'>
     <input type='file' name='fichier1' id='fichier' size='60' /><br />
     <input type='file' name='fichier2' id='fichier' size='60' /><br />
     <input type='submit' class='bouton' id='boutEnvoyer' value='Envoyer' />
</form>
</body></html>

witch is equivalent to my bash command /usr/bin/curl -F fichier1=@/path_to_file1 -F fichier2=@/path_to_file2 http://MyUrl.com/backups/uploadLogs.php

1 个答案:

答案 0 :(得分:0)

好,我找到了解决方法

我已经下载了powershell 6.2版本powershell 6.2 preview,现在-Form参数有效了!

我将代码更正为

 $Uri = 'http://MyUrl.com/backups/uploadLogsVms.php'
$Form = @{
    fichier1     = Get-Item -Path 'C:\users\ADM_2N\Desktop\PROTECTED_VMS_JOB_SCHEDULE_2018_12_10.csv'
    fichier2     = Get-Item -Path 'C:\users\adm_2n\Desktop\backup_points.csv'

}
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form