I've seen many examples of Invoke-Webrequest and I've already had some success with it myself, however, one site where I was trying to automate my login just hasn't worked no matter what I try. Here is the code:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # This is required so that HTTPS requests won't fail with Invoke-WebRequest
$r = Invoke-WebRequest -Uri https://order.swisschalet.com -SessionVariable sc
$r.forms[0].fields['form-login-header-email'] = "MyEmail"
$r.forms[0].fields['form-login-header-password'] = 'MyPassword'
$a = Invoke-WebRequest -Uri ("https://order.swisschalet.com" + $r.forms[0].Action) -WebSession $sc -Method POST -Body $r.forms[0]
I have tried using Fiddler 4 to analyze what is going on but it has only confused me even more. When I manually go to the website Fiddler shows 'email' and 'password' fields that were posted rather than what originally came back in the forms which is 'form-login-header-email' and 'form-login-header-password'. However, even if I try to create these new fields and POST them it still doesn't work. Fiddler shows that going to the website manually also creates some kind of synchronization token called 'org.codehaus.groovy.grails.SYNCHRONIZER_TOKEN'.
I am beginning to wonder if Invoke-WebRequest is simply incompatible with this site as I can never get the expected response where I can find my name in the $a.parsedHTML.DocumentElement.InnerText. Instead, when I view this I simply get the full page back telling me that my session has already expired.
I started to try this with the IE Com Object as well but this also did not seem to work. Am I missing something or is it just the way this site has been made? I've been struggling with this (really just to learn) for a couple of days now.
Thanks for any help!
If you send what it sends, to the place it sends it, you get logged in:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # This is required so that HTTPS requests won't fail with Invoke-WebRequest
$r = Invoke-WebRequest -Uri https://order.swisschalet.com -SessionVariable sc
$form = #{
delegate='login'
id='null'
mode='save'
target='#form-login'
email='email#example.com'
password='password'
}
$a = Invoke-WebRequest -Uri "https://order.swisschalet.com/order/auth/index" -WebSession $sc -Method POST -Body $form
and it says
<span class="welcome-back">Welcome, <span class="username">TestingSome StuffForStackOverflo</span>
in the $a.RawContent result. So I guess it's not incompatible.
Related
We have a php site that uploads pictures to server, simple jpg file with correct naming. Problem is that we need to upload sometimes couple of hundred of them at a time, but php accepts only 1 at a time, renames them and uploads. I have done file operations in PS quite well, but fail to upload.
PHP part related to upload (as far as I can tell) looks like this: <form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">
I checked Google, related topics here as well, and got to this:
$uri = "http://example.com/"
$pwd = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('myuser', $pwd)
$contentType = "multipart/form-data"
$body = #{
"FileName" = Get-Content($uploadPath) -Raw
}
Invoke-WebRequest -Uri $uri -Method Post -ContentType $contentType -Body $body
I have checked $uploadPath and it is correct C:\Folder\file.jpg. I use credentials that I use to log in to site where I can upload these pictures via GUI.
I have tried switching between POST and PUT, with no changes.
Replacing http://example.com with http://example.com/file.jpg also provided no difference. Unsure, which is correct way to use POST.
We have McAffe web gateway in company, but I'm running script with user that bypasses it, so it's not causing this.
Current error message that I'm getting is:
"Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a receive."
Any help would be greatly appreciated! And sorry if this has been already solved and I've simply missed an entry!
P.S. I have also tried this - Powershell script to Upload log file from local system to http URL, and it returns Exception calling "UploadFile" with "3" argument(s): "An exception occurred during a WebClient request."
A year late, so you probably don't need the answer anymore, but what worked for me was:
Invoke-RestMethod -Uri $uri -Method Post -InFile $uploadPath -UseDefaultCredentials
Mine needed to use windows auth as the currently logged in user... From this other answer, looks like you can use -cred in some cases and have to build out an Authorization header in others.
Even later post but these Invoke-RestMethod solutions (and a few others) didn't work for me. My service endpoint was failing with missing boundaries and other "content" issues, depending on my trial and error.
I tried using WebClient's Uploadfile() functionality and it worked for me.
Thankfully, it's concise.
$wc = New-Object System.Net.WebClient
$resp = $wc.UploadFile($uri,$uploadPath)
I am trying to download package from the below link using powershell.
https://www.tenable.com/downloads/nessus-agents
i do not have direct link for these package also when i click on download it ask to agree. I was able to do it on Linux using command shown below. Kindly advise how can i do it in windows.
"wget --no-check-certificate --post-data='accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm' 'https://www.tenable.com/downloads/nessus-agents' -O NessusAgent-7.4.2-amzn.x86_64.rpm"
could not find anything tried option with invoke-webrequest
Invoke-RestMethod -Uri 'https://www.tenable.com/downloads/nessus-agents'
There's a GET query string parameter that indicates acceptance.
Simply add i_agree_to_tenable_license_agreement=true to your query string parameters.
Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents/downloads/9762/download?i_agree_to_tenable_license_agreement=true' -OutFile 'NessusAgent-7.4.2-x64.msi'
You can easily get the IDs of the other files from their API endpoint like so:
(Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents' | ConvertFrom-Json).downloads | Format-Table -AutoSize
This is similar syntax in Powershell, but it's just downloading a file with contents "OK".
$body = 'accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm'
$uri = 'https://www.tenable.com/downloads/nessus-agents'
$resp = Invoke-WebRequest -Method Post -Body $body -Uri $uri -OutFile .\NessusAgent-7.4.2-amzn.x86_64.rpm
Maybe the "sid" variable needs to change per request.
We have a php site that uploads pictures to server, simple jpg file with correct naming. Problem is that we need to upload sometimes couple of hundred of them at a time, but php accepts only 1 at a time, renames them and uploads. I have done file operations in PS quite well, but fail to upload.
PHP part related to upload (as far as I can tell) looks like this: <form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">
I checked Google, related topics here as well, and got to this:
$uri = "http://example.com/"
$pwd = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('myuser', $pwd)
$contentType = "multipart/form-data"
$body = #{
"FileName" = Get-Content($uploadPath) -Raw
}
Invoke-WebRequest -Uri $uri -Method Post -ContentType $contentType -Body $body
I have checked $uploadPath and it is correct C:\Folder\file.jpg. I use credentials that I use to log in to site where I can upload these pictures via GUI.
I have tried switching between POST and PUT, with no changes.
Replacing http://example.com with http://example.com/file.jpg also provided no difference. Unsure, which is correct way to use POST.
We have McAffe web gateway in company, but I'm running script with user that bypasses it, so it's not causing this.
Current error message that I'm getting is:
"Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a receive."
Any help would be greatly appreciated! And sorry if this has been already solved and I've simply missed an entry!
P.S. I have also tried this - Powershell script to Upload log file from local system to http URL, and it returns Exception calling "UploadFile" with "3" argument(s): "An exception occurred during a WebClient request."
A year late, so you probably don't need the answer anymore, but what worked for me was:
Invoke-RestMethod -Uri $uri -Method Post -InFile $uploadPath -UseDefaultCredentials
Mine needed to use windows auth as the currently logged in user... From this other answer, looks like you can use -cred in some cases and have to build out an Authorization header in others.
Even later post but these Invoke-RestMethod solutions (and a few others) didn't work for me. My service endpoint was failing with missing boundaries and other "content" issues, depending on my trial and error.
I tried using WebClient's Uploadfile() functionality and it worked for me.
Thankfully, it's concise.
$wc = New-Object System.Net.WebClient
$resp = $wc.UploadFile($uri,$uploadPath)
I am trying to submit a form using invoke-webrequst cmdlet, and this is the code
$postParams = #{regno='1234567';dob='01/01/1997';B1='Get Marks'}
$response = Invoke-Webrequest -Uri ("http://studentresulsts/res.asp") -Body $postParams -Method Post -Debug -OutFile out.html
VERBOSE: POST http://studentresulsts/res.asp with -1-byte payload
VERBOSE: received 13-byte response of content type text/html
The $response comes back as 'Access Denied' (13-byte response)
The payload length is shown as 1-byte while $postParams is clearly more than that. Wondering if that's the reason I am getting 'Access Denied'.
Checked the form manually in browser and it works fine with correct field values.
I am using powershell 4.0
Answer : Okay, I was missing referrer URL in the header which the server was looking for, then I included referrer and it works fine :) . My new script looks like this. Thanks for your help...
$postParams = #{regno='1234567';dob='01/01/1997';B1='Get Marks'}
$headervals = #{'Referer'='http://studentresulsts/gdslplus/gdslform.htm';'Content-Type'='application/x-www-form-urlencoded'}
$response = Invoke-Webrequest -Uri ("http://studentresulsts/res.asp") -Body $postParams -Method Post -Debug -OutFile out.html
The fact that you don't have to manually authenticate does not mean the access isn't verified(e.g. trusted sites in IE).
Perhaps you are missing a crucial header?
I have seen downloading a file with the webclient for powershell. Problem is that the request url redirects multiple times before the actual file is presented. it's an ssrs web interface with a query string meathod Download=bla.csv
any idea how to accomplish this?
I think you can use this script on poshcode.org or try to take hints using System.Net.CookieContainer to avoid falling in a redirect loop.
I had a similar scenario where my URL is redirected and then we need to download the file. What I did was get the redirected URL and parse for the http address and again invoked with the redirected URL. Below code worked for me.
$username = "u"
$password = "p"
$auth=$username+":"+$password
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$latestArtifactURL = Invoke-WebRequest $url -Headers #{Authorization = "Basic $EncodedPassword"} -MaximumRedirection 0 -ErrorAction SilentlyContinue
$URLIndex = "$latestArtifactURL".IndexOf('http:')
$redirectedURL = "$latestArtifactURL".SubString("$URLIndex")
Invoke-WebRequest $redirectedURL -Headers #{Authorization = "Basic $EncodedPassword"} -Outfile "App.zip"
You can also use the webclient to download the file. Hope this helps.