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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Classic1Database AdministratorAuthor Commented:
Thanks for the quick response! Option 3 worked for me...

Much appreciated,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.