question

SundaresanChandrakanth-2366 avatar image
0 Votes"
SundaresanChandrakanth-2366 asked SundaresanChandrakanth-2366 rolled back

Powershell Invoke web request find and pass parameter

hi

I wish to create a Powershell script to automate a download file from a URL. The challenge is that the website takes a parameter as input as Account number and then it gives the file download link. Everyday there is a new file it creates with file name as date and give download link. When i try to inspect the download link, it says javascript.

  1. how to find the parameter from the website it takes to get in to the download urL. The url portion does not change after passing the input in the account number.

  2. How to form a script with input of this account number with invoke-webrequest?

  3. how to create a download url and pass it to the invoke-webrequest command?

Please help as this is something new to me in powershell to try for a URL based automation.



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @SundaresanChandrakanth-2366 ,

maybe this helps to get started:

 $account = "abaumgarten42"
 $url = 'https://github.com/'
 $uri = "$url$account"
 Invoke-WebRequest -URI $uri


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @SundaresanChandrakanth-2366,

This may be more complicated than it seems.

First you would need to find the field, inspecting the page with F12 and finding the <Input> line and the -ID tag (for example -ID "Account Number", or similar) and the Button ID by inspecting the element as well.

Then you can use the script to navigate there

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # To Make it visible
$AccountNumber="12345678" # defines the Account variable
$ie.Navigate("https://<URL>")

$accountnamefield = $ie.document.getElementByID('<the Input ID tag>') #Gets the element by ID
$accountnamefield.value = $AccountNumber #sets the variable

$Link = $ie.document.getElementByID('<Continue Button tag ID>')
$Link.click() # this will continue to the Link generated

Then using the Invoke-Webrequest you should be able to get the link generated.

Hope this helps in your case,
Best regards,



--If the the reply is helpful, please Upvote and Accept as answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SundaresanChandrakanth-2366 avatar image
0 Votes"
SundaresanChandrakanth-2366 answered SundaresanChandrakanth-2366 edited

Hi I tried your way as below but get error for the same.

You cannot call a method on a null-valued expression.
At E:\testbrowse.ps1:6 char:1
+ $accountnamefield = $ie.document.getElementByID('txtNumber') #Gets th ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At E:\testbrowse.ps1:7 char:1
+ $accountnamefield.value = $AccountNumber #sets the variable
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

Exception from HRESULT: 0x800A01B6
At E:\testbrowse.ps1:9 char:1
+ $Link = $ie.document.getElementByID('btnSubmit')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException

You cannot call a method on a null-valued expression.
At E:\testbrowse.ps1:10 char:1
+ $Link.click() # this will continue to the Link generated
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SundaresanChandrakanth-2366 avatar image
0 Votes"
SundaresanChandrakanth-2366 answered SundaresanChandrakanth-2366 rolled back

For account number

 input id="txtNumber" type="tel" maxlength="9" placeholder="Enter your 9 digit Account No."
    
 button id="btnSubmit" 



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.