Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use Invoke-RestMethod with Import-Csv to loop through URLs

I’m attempting to dynamically create about 50 QR Codes using GoQR.me’s API. I have all the source and destination URLs in a CSV file QRCodes.csv formatted as:

source, destination
https://api.qrservers.com/v1/..., C:\Users\...\QR1.png

I originally attempted to use PowerShell’s Start-BitsTransfer

Import-Csv QRCodes.csv | Start-BitsTransfer

But because the server doesn’t return Content-Length in the header of the HTTP reply, Start-BitsTransfer fails.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How would I do something similar using Invoke-RestMethod or Invoke-WebReqest instead? I can easily do one request at a time, but can’t figure out how to loop through the CSV. (I’m a relative noob at PowerShell)

>Solution :

Assuming the source cell contains all the data required by the API endpoint, use ForEach-Object to iterate over the list of CSV records and then call Invoke-WebRequest:

Import-Csv QRCodes.csv | ForEach-Object {
    Invoke-WebRequest -Uri $_.source -OutFile $_.destination
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading