Share via

Powershell Invoke web request find and pass parameter

Sundaresan Chandrakanth 11 Reputation points
2021-09-21T14:53:18.547+00:00

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 for business | Windows Server | User experience | PowerShell
0 comments No comments

4 answers

Sort by: Most helpful
  1. Sundaresan Chandrakanth 11 Reputation points
    2021-09-22T09:17:32.903+00:00

    For account number

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

    Was this answer helpful?

    0 comments No comments

  2. Sundaresan Chandrakanth 11 Reputation points
    2021-09-22T09:14:53.643+00:00

    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

    Was this answer helpful?

    0 comments No comments

  3. Limitless Technology 40,101 Reputation points
    2021-09-22T07:22:26.31+00:00

    Hello @Sundaresan Chandrakanth ,

    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--

    Was this answer helpful?

    0 comments No comments

  4. Andreas Baumgarten 132K Reputation points MVP Volunteer Moderator
    2021-09-21T15:16:38.077+00:00

    Hi @Sundaresan Chandrakanth ,

    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

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.