简体   繁体   English

在 Powershell 脚本中上传 csv 文件时出错

[英]Error when Uploading csv file in Powershell Script

I have the below powershell script which is returning the value as success but it is not getting uploaded to the target system.我有以下 powershell 脚本,该脚本将值返回为成功,但没有上传到目标系统。

$Uri ="https://example.com"
$Auth_Token = 'xxxx'
$headers = @{"Authorization" = "Bearer $($Auth_Token)"}
$file ='C:\Users\r\feedfile.csv'
$fields =@{}
$fields.add("file",$file)
$FieldJson = $fields | ConvertTo-Json

Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -ContentType "multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p" -Body $FieldJson

To me without an error message, my guess is that your not creating a json output from your csv file, as your not importing the csv file and also you content type is not correct. To me without an error message, my guess is that your not creating a json output from your csv file, as your not importing the csv file and also you content type is not correct. it should be ' -ContentType = "application/json" ' and not '"multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p"'它应该是 ' -ContentType = "application/json" ' 而不是 '"multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p"'

Steps To Test CSV to Json Conversion测试 CSV 到 Json 转换的步骤

  1. Generate File Path生成文件路径
  2. Import CSV File导入 CSV 文件
  3. Create PowerShell Array创建 PowerShell 阵列
  4. Add CSV data to PowerShell Array将 CSV 数据添加到 PowerShell 数组
  5. Convert PowerShell Array To Json将 PowerShell 数组转换为 Json

Example Script To Test CSV to Json Conversion I've hashed out the parts used for your invoke-restmethod.测试 CSV 到 Json 转换的示例脚本我已经列出了用于您的 invoke-rest 方法的部分。 To test the create json from csv file (see picture for validation of the below & amend accordingly to test yourself).要测试从 csv 文件创建 json (参见图片以验证以下内容并进行相应修改以测试自己)。

#$Uri ="https://example.com"
#$Auth_Token = 'xxxx'
#$headers = @{"Authorization" = "Bearer $($Auth_Token)"}

#Generate File Path
$folderspath = 'C:\Test'
$csvfilename = 'info.csv'
$csvfilepath = $folderspath + "\" + $csvfilename
$csvfilepath = $csvfilepath.ToString()

$jsonFilename = 'info.json'
$jsonfilepath = $folderspath + "\" + $jsonFilename
$jsonfilepath = $jsonfilepath.ToString()

#Create A Json File From CSV File
#import-csv $csvfilepath | ConvertTo-Json | Add-Content -Path $jsonfilepath

#Import CSV
$csvfile = Import-CSV -Path $csvfilepath

#PowerShell Array
$fields =@{}

#Get csv file
Write-Host "Import Full CSV File For Json Test"
$fields = $csvfile

#Get Column Headers for test
#Write-Host "Import CSV File Headers For Json Test"
#$fields = ($csvfile[0].psobject.Properties).name
#$fields

#Convert To Json
$FieldJson = $fields | ConvertTo-Json
$FieldJson


#Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -ContentType "multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p" -Body $FieldJson

#Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -ContentType "application/json" -Body $FieldJson

Json 输出测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM