Uploading a .csv file to a https site using PowerShell

Hi there,
Was wondering if someone could help me out here...in the past, we automated a ftp process to one of our customers, using a batch file and the ftpcmd.dat command. Now, they have switched to a https site, and as a result, broke our current process.

Doing some searching, I was able to find links using PowerShell as an alternative, particularly, the Invoke-WebRequest cmdlet. Going step-by-step, I was able to connect to the site; here's what I did:

# The first command uses the **Invoke-WebRequest** cmdlet to send a sign-in request. The command specifies a value of "FB" for the value of the *SessionVariable* parameter, and saves the result in the $R variable.When the command completes, the $R variable contains an **HtmlWebResponseObject** and the $FB variable contains a **WebRequestSession** object.

$R=Invoke-WebRequest https://website -SessionVariable fb

# The second command shows the **WebRequestSession** object in the $FB variable.

# The third command gets the first form in the **Forms** property of the HTTP response object in the $R variable, and saves it in the $Form variable.
$Form = $R.Forms[0]

# The fourth command pipes the properties of the form in the $Form variable into a list by using the Format-List cmdlet.
$Form | Format-List

# The fifth command displays the keys and values in the hash table (dictionary) object in the Fields property of the form.

# The sixth and seventh commands populate the values of the email and pass keys of the hash table in the **Fields** property of the form. You can replace the email and password with values that you want to use.

# The eighth command uses the **Invoke-WebRequest** cmdlet to sign into the Facebook web service.The value of the *Uri* parameter is the value of the **Action** property of the form. The **WebRequestSession** object in the $FB variable (the session variable specified in the first command) is now the value of the *WebSession* parameter. The value of the *Body* parameter is the hash table in the Fields property of the form and the value of the *Method* parameter is POST. The command saves the output in the $R variable.

$R=Invoke-WebRequest -Uri ("https://website" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields

# When the command finishes, the **StatusDescription** property of the web response object in the $R variable indicates that the user is signed in successfully.


Open in new window

Using this, I was able to sign in...trying to upload the file, this is where I got errors...I tried creating a variable, which would store the path of the file:

$picPath = "D:\File path\Document.csv"

# Then I tried to use the Invoke-WebRequest cmdlet, but got an error: Unauthorized

$R=Invoke-WebRequest -Uri ("https://website" + $Form.Action) -Method Put -Infile $picPath

# I used the $Form | Format-List  and saw the Method was Post, so I changed it to Post and got a different error: The selected resource was not found

$R=Invoke-WebRequest -Uri ("https://website" + $Form.Action) -Method Post -Infile $picPath

Open in new window

Upload Errors
I also embedded the field names on the 2nd form, once I signed in successfully...

2nd Form Settings
Just wondering if I'm on the right path...please let me know if you need additional info/details...

Much appreciated,
Classic1Database AdministratorAsked:
Who is Participating?
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
2 Suggestions:
1. Add the conflictive part into a Try/catch.
2. try it with userdefaultcredential, like:
Invoke-Webrequest -uri "http://site" -UseDefaultCredential

3 if that one doesn't work try the credentials of tfhe web page:

$client = New-Object System.Net.Webclient
$client.Credentials = New-Object System.Net.NetworkCredential("user","pass")

4. last Option
Invoke-WebRequest -Uri "URL" -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))} -Method Get

Here's the s source: https://social.technet.microsoft.com/Forums/windows/en-US/2e1058b7-9eb1-42f8-826f-36b50b560518/invokewebrequest-401-unauthorized?forum=winserverpowershell
Classic1Database AdministratorAuthor Commented:
Thanks for the quick response! Option 3 worked for me...

Much appreciated,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.