Am trying to incorporate some C# code into my PowerShell, as I am still learning C# and getting a foothold on learning a new language.
I am able to open a file locally on my computer and read it line by line. But cannot seem to work out how to open a file from the internet and parse it line by line.
This works to read the file line by line, and allows me to take action on the line items in the loop.
$f = 'C:\tmp\tinyUF.txt'
$reader = [System.IO.File]::OpenText($f)
while (-not($reader.EndOfStream)) {
$line = $reader.ReadLine()
}
$x.Close()
This doesn't work, so I did some research and found that [System.IO.File]
is not going to be able to read $f
as a feed.
$f = (Invoke-WebRequest -Uri https://algs4.cs.princeton.edu/15uf/tinyUF.txt).Content
$reader = [System.IO.File]::OpenText($f)
while (-not($reader.EndOfStream)) {
$line = $reader.ReadLine()
}
$x.Close()
So I looked at different ways to solve my own problem and learn along the way:
1. Using [System.Net.WebRequest]
Created a $reader
object and pointed it to the URL. But I can't see a method that allows me to read the data. I was hoping it would work like Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content
and I would be able to parse the data, but that didn't work out.
$reader = [System.Net.WebRequest]::Create('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')
2. Using [System.Net.WebRequest]
Created a $reader
object and pointed it to the URL. But I can't see a method that allows me to read the data. I was hoping it would work like Invoke-WebRequest -Uri 'https://algs4.cs.princeton.edu/15uf/tinyUF.txt').Content
and I would be able to parse the data, but that didn't work out.
$reader = [System.Net.WebRequest]::Create('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')
3. Using [System.Net.WebClient]
I thought maybe I could create a WebClient and point it to the URI as shown here
So I put that together, and got the error on the second line:
$reader = [System.Net.WebClient]::new('https://algs4.cs.princeton.edu/15uf/tinyUF.txt')
MethodException: Cannot find an overload for "new" and the argument count: "1".
I tried some of the other solutions listed there, but didn't quite understand them.