1

I want to compare local text file with online file via PowerShell, the content of two files are the same.

I know I have to use Compare-Object cmdlet to compare two files, and I found I could get the content of online file by Invoke-WebRequest cmdlet (https://www.quora.com/How-do-I-download-URL-content-using-Get-Content-in-PowerShell-Script). But it's not working as expected. It just output the file content of the online version.

$item1 = cat $path
$item2 = Invoke-WebRequest -Uri $URL | select -ExpandProperty Content

# No working as expected
Compare-Object -ReferenceObject $item1 -DifferenceObject $item2 

Updates

After debugging, I found out that the type of return value is System.Array when using Get-Content (cat), but when using Invoke-WebRequest it's String.

PS C:\> $item1.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\> $item2.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kevin Xiong
  • 153
  • 13
  • 1
    **"but It's not working as expected"** What is it doing? Any output at all? Any errors? – boxdog Jul 10 '19 at 12:32
  • @boxdog I have updated the description a little bit – Kevin Xiong Jul 10 '19 at 12:42
  • Your approach will not lead to a satisfactory result. Compare Object really only helps compare the properties of two objects, i.e. does one object have a property that the other object does not have and it checks when both objects have a property of the same name if its value is differs. If you want to see the exact differences between the files you're better of using a tool such as diff or git diff. However, if you only want to know if the files are the same then you can download both and run `Get-FileHash` to compare their checksums. If they're same the files are identical. – megamorf Jul 10 '19 at 12:46
  • @megamorf That is incorrect. `Compare-Object` is perfectly suitable for comparing 2 arrays of strings to get the differing lines. – Ansgar Wiechers Jul 10 '19 at 12:48

1 Answers1

3

Invoke-WebRequest returns the content of the requested web page as a single string. Split the string at newlines and you'll be able to compare it to the data from the text file (Get-Content produces an array of strings by default).

Compare-Object -ReferenceObject $item1 -DifferenceObject ($item2 -split '\r?\n')
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • you are right, I tried your code, it's not working, update it a little bit, it works now. ```powershell Compare-Object -ReferenceObject $item1 -DifferenceObject ($item2 -split '\r\n') ``` – Kevin Xiong Jul 10 '19 at 13:07
  • However, the depends on the endline, need to make this scripts working for "LF" and "CRLF" – Kevin Xiong Jul 10 '19 at 13:12
  • I found the regexp usage in "-split" is a little weird, "(" or ")" can't be used inside'', have to change it to: `Compare-Object -ReferenceObject $item1 -DifferenceObject ($item2 -split '\r?\n')` – Kevin Xiong Jul 10 '19 at 14:01
  • @KevinXiong `(?<=...)` is a positive lookbehind assertion. But apparently it doesn't work the way I expected (it seems to ignore the CR characters). Not sure why. Amended my answer. – Ansgar Wiechers Jul 10 '19 at 14:19
  • I know lookbehind assertion in regexp, but it's not woking in powershell. not sure if it's a bug or by design. – Kevin Xiong Jul 10 '19 at 14:25
  • It usually does work, just not in this case. Like I said, not sure why that is. – Ansgar Wiechers Jul 10 '19 at 14:29
  • I got it, we actually don't need to use lookbehind assertion in this case, lookbehind assertion is what we need it in the match, we don't need it while processing the string. got stupid when I see you use regexp... – Kevin Xiong Jul 10 '19 at 16:15