Powershell上传文件并通过NTLM进行身份验证

时间:2016-11-03 12:35:36

标签: shell powershell ntlm

请通过Powershell 上传excel文件我需要帮助。 我需要编写脚本,POST现有的excel(根据外部系统规则格式良好)到该系统的URI。 脚本需要通过NTLM (在Apache上)进行身份验证,然后发布数据。

我尝试过这样的事情:

Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentType "multipart/form-data" -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))} 

其中$ uri类似于http://[servername]/[applicationame]/apix/marketdata/upload

和$ filePath是C:\ Users .. \ Documents \ Market_data.xlsx

错误:

Invoke-WebRequest : {"status":"ERROR","message":"HTTP 400 Bad Request","additionalMessages":[]}
At C:\Users\..\script.ps1:48 char:1
+ Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentTy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

此Curl.exe命令正在运行,因此我需要将其重新键入PS curl

curl.exe -X POST -H "Pragma: no-cache" -H "Origin: http://[server]" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Accept: */*" -H "Cache-Control: no-cache" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Referer: http://[server]/" -F "Content-Type=application/vnd.ms-excel" -F "File=@$filePath" -F "filename=$filePath" "http://[servername]/[applicationname]/apix/marketdata/upload?User=[DOMAIN]%255C[USER]" --ntlm --negotiate -u 'DOMAIN\user:password'

1 个答案:

答案 0 :(得分:0)

好的,所以我现在没有能力进行测试,但这应该可以使用-credential参数并传入你需要使用的任何凭据对象。