Reply

Powershell CSV IMPORT Issue

New Member
Posts: 2
8503     0

Would anyone there be able to provide some insight into importing a CSV via Powershell?

 

I've been through all of the python examples and used them as a basis for the Powershell version that I'm working on and have all of the steps working except that the uploaded CSV is always zero length (and therefore no data gets processed)

 

This is after successfully initializing the upload, getting the token and upload url, etc. 

 

The following code is being used to upload the CSV:

 

~~~

 

$URI = 'https://gridmaster/wapi/v2.7/fileop?_function=csv_import'

$file = "somefile.csv"

$file_contents = Get-Content -Path $file



$params = @{ 
  "name"=$file
  "filedata"=$file_contents

}


$body = $params|ConvertTo-Json


$upload = Invoke-RestMethod -Uri $URI -Method POST -Body $params -WebSession $Session

~~~

 

 

If I upload the CSV file manually via the GUI, the import works.  If I use python, it works.  Powershell gives me an empty file on the server, so I suspect that there's something wrong with the way I'm trying to load the contents. 

 

Any Powershell gurus out there care to chime in, I'd be grateful for pointers!

 

 

Re: Powershell CSV IMPORT Issue

New Member
Posts: 2
8504     0

So for those of you that may run into this same issue, I've found a workaround...

 

No combination of Invoke-RestMethod or Invoke-WebRequest would result in the contents of the CSV getting uploaded to the Grid.  I always ended up with a zero-length file.

 

In the end, I switched over to using the .Net webclient and uploading the file that way. 

 

working code block:

~~~

$URI = 'https://gridmaster/wapi/v2.7/fileop?_function=csv_import'

$filepath = "$$(PSScriptroot)\somefile.csv"
$file = Get-Item -Path $filepath

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = Get-Credential 
$webclient.UploadFile($URI, "POST", $filepath)

~~~

 

This results in the CSV (and it's contents) getting uploaded to the temp location so that it can then be processed with a follow up Invoke-Restmethod to the 'fileop?_function=csv_import' url along with the token acquired previously.

 

I can't say that this is an elegant solution, nor do I like having to resort to the .Net webclient... but it works. 

 

Still, if anyone has a better way of dealing with this in Powershell, please by all means let me know!

 

Thanks!

Showing results for 
Search instead for 
Did you mean: 

Recommended for You