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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,572 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 111.5K Reputation points MVP
    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

    0 comments No comments

  2. Limitless Technology 39,696 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--

    0 comments No comments

  3. 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
    0 comments No comments

  4. 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" 
    
    0 comments No comments

Your answer

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