Login and download a file in web using powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-2622440%22%20slang%3D%22en-US%22%3ELogin%20and%20download%20a%20file%20in%20web%20using%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2622440%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20quite%20new%20to%20PowerShell%2C%20what%20I%20am%20trying%20to%20do%20is%20to%20create%20script%20automation%20where%20I%20can%20log%20in%20to%20my%20account%20and%20click%20the%20download%20button%20from%20a%20website%20using%20Invoke-Web-Request.%20But%20whenever%20I%20open%20the%20downloaded%20file(CSV).%20It%20will%20just%20contain%20the%20HTML%20script%20from%20the%20login%20page.%3CBR%20%2F%3E%3CBR%20%2F%3EHere%20are%20the%20codes%20I%20used%3A%3CBR%20%2F%3EPowershell%3C%2FP%3E%24web%20%3D%20Invoke-WebRequest%20-uri%20%22%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2F%3C%2FA%3E%22%20-SessionVariable%20sess%20%24form%20%3D%20%24web.forms%5B0%5D%20%24form.fields%5B%22id_username%22%5D%20%3D%20%22user123%22%20%24form.Fields%5B%22id_password%22%5D%20%3D%20%22pass123%22%20%24in%20%3D%20Invoke-WebRequest%20-uri%20(%22%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2F%3C%2FA%3E%22%2B%24form.Action)%20-WebSession%20%24sess%20-Method%20POST%20-Body%20%24form.Fields%20Invoke-WebRequest%20-Uri%20%22%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2Fusers%2Freport%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2Fusers%2Freport%3C%2FA%3E%22%20-OutFile%20%22C%3A%5Cpath%5Cusers.csv%22%3CP%3EHTML%20CODES%3C%2FP%3E%3CP%3ELogin%20form%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2F%3A%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2F%3A%3C%2FA%3E%3C%2FP%3E%20%3CDIV%20class%3D%22%26quot%3Blogin-panel__username%26quot%3B%22%3E%20%3CLABEL%20for%3D%22%26quot%3Bid_username%26quot%3B%22%3EUsername%3A%3C%2FLABEL%3E%20%3CINPUT%20id%3D%22%26quot%3Bid_username%26quot%3B%22%20name%3D%22%26quot%3Busername%26quot%3B%22%20placeholder%3D%22%26quot%3BEnter%22%20your%3D%22%22%20username%3D%22%22%20%2F%3E%20%3C%2FDIV%3E%20%3CDIV%20class%3D%22%26quot%3Blogin-panel__password%26quot%3B%22%3E%20%3CLABEL%20for%3D%22%26quot%3Bid_password%26quot%3B%22%3EPassword%3A%3C%2FLABEL%3E%20%3CINPUT%20id%3D%22%26quot%3Bid_password%26quot%3B%22%20name%3D%22%26quot%3Bpassword%26quot%3B%22%20placeholder%3D%22%26quot%3BEnter%22%20your%3D%22%22%20password%3D%22%22%20%2F%3E%20%3C%2FDIV%3E%20%3CDIV%20class%3D%22%26quot%3Blogin-panel__submit-wrapper%26quot%3B%22%3E%20%3CINPUT%20class%3D%22%26quot%3Bbtn%22%20btn-primary%3D%22%22%20btn-lg%3D%22%22%20btn-block%3D%22%22%20%2F%3E%20%3C%2FDIV%3E%3CP%3ECode%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2Fusers%2F%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2Fusers%2F%3C%2FA%3E%26nbsp%3Bcontaining%20the%20download%20button%20that%20will%20redirect%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fprivatewebsite.com%2Fusers%2Freport%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fprivatewebsite.com%2Fusers%2Freport%3C%2FA%3E%26nbsp%3Bto%20download%20the%20file%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%20%3CUL%20class%3D%22%26quot%3Baccount__actions%22%20button-group%3D%22%22%20right%3D%22%22%3E%20%3CLI%3E%20%3CA%20href%3D%22%26quot%3Bhttps%3A%2F%2Fprivatewebsite.com%2Fusers%2Freport%26quot%3B%22%20class%3D%22%26quot%3Bbtn%22%20btn-lg%3D%22%22%20btn-primary%3D%22%22%20hz-hint%3D%22%22%20hz-hint--bottom%3D%22%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%3CI%20class%3D%22%26quot%3Bfa%22%20fa-cloud-download-alt%3D%22%22%3E%3C%2FI%3E%20%3C%2FA%3E%20%3C%2FLI%3E%20%3CLI%3E%20%3CA%20href%3D%22%26quot%3Bhttps%3A%2F%2Fprivatewebsite.com%2Fcreate%26quot%3B%22%20class%3D%22%26quot%3B%22%20btn%3D%22%22%20btn-lg%3D%22%22%20btn-primary%3D%22%22%20hz-hint%3D%22%22%20hz-hint--bottom%3D%22%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%3CI%20class%3D%22%26quot%3Bfa%22%20fa-plus%3D%22%22%3E%3C%2FI%3E%20%3C%2FA%3E%20%3C%2FLI%3E%20%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2622440%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

I am quite new to PowerShell, what I am trying to do is to create script automation where I can log in to my account and click the download button from a website using Invoke-Web-Request. But whenever I open the downloaded file(CSV). It will just contain the HTML script from the login page.

Here are the codes I used:
Powershell

$web = Invoke-WebRequest -uri "https://privatewebsite.com/" -SessionVariable sess    
$form = $web.forms[0]
$form.fields["id_username"] = "user123"
$form.Fields["id_password"] = "pass123"

$in = Invoke-WebRequest -uri ("https://privatewebsite.com/"+$form.Action) -WebSession $sess -Method POST -Body $form.Fields

Invoke-WebRequest -Uri "https://privatewebsite.com/users/report" -OutFile "C:\path\users.csv"

HTML CODES

Login form from https://privatewebsite.com/:

<div class="login-panel__username">
          <label for="id_username">Username:</label>
          <input id="id_username" name="username" placeholder="Enter your username " type="text" />
          
        </div>

        <div class="login-panel__password">
          <label for="id_password">Password:</label>
          <input id="id_password" name="password" placeholder="Enter your password" type="password" />
          
        </div>

        <div class="login-panel__submit-wrapper">
          <input
             class="btn btn-primary btn-lg btn-block"
             type="submit"
             value="Login"
             >
        </div>

Code from https://privatewebsite.com/users/ containing the download button that will redirect to https://privatewebsite.com/users/report to download the file

 

<ul class="account__actions button-group right">
    
    <li>
      <a href="https://privatewebsite.com/users/report"
         class="btn btn-lg btn-primary hz-hint hz-hint--bottom"
         data-hint="Download user report"
      >
        <i class="fa fa-cloud-download-alt"></i>
      </a>
    </li>
    
    
    <li>
      <a
        
          href="https://privatewebsite.com/create"
        
          class="
            btn btn-lg btn-primary
            
            hz-hint hz-hint--bottom
            
          "
          data-hint="Create user"
        >
        <i class="fa fa-plus"></i>
      </a>
    </li>
    
  </ul>

 

Thanks in advance.

 

 

 

 

 

 

 

 

0 Replies