Use PowerShell compare a local text file with a text file on the Web are the same

Comparative local text file is the same as generally used in two ways PowerShell: 1 by Get-FileHash this command, the hash compare two files are the same; 2 by. The Compare-Object command, compare two files per line. They are the same.

Compare local text files and text files on the Web is the same two kinds of ideas, but first of all to deal with the files on the web. Processing the web document also clearly two approaches: 1 to give the content of the web document (. The Invoke-the WebRequest ), compared directly in memory; 2 content of the web to obtain the file, then save the file locally, the file is converted to local. comparison between. This method requires only after obtaining the contents of web documents, plus step file write operations ( New-Item , the Add-Content ) can be, nothing to say, this article is mainly about the first comparison of two ways ways, in order to easy to identify the correctness of the program, the contents of the two documents is the same here.

1. Compare two hash files are the same

1   # Get local file hash (using the MD5) 
2   $ path = " C: \ local.txt " 
. 3   $ hashLocal = the Get-FileHash -Path $ path - Algorithm the MD5
 . 4   the Write-the Output $ hashLocal 
. 5  
. 6   $ URL = " XXX " 
7   # set" -ExpandProperty "to fully return the text content 
8   $ cotent = the Invoke-WebRequest -uri $ url | the SELECT - ExpandProperty content
 9   # into Char array, put in MemoryStream 
10   $ charArray = $ cotent.ToCharArray ()
 . 11   $ Stream = [System.IO.MemoryStream] :: new new ( $ charArray )
 12 is   # the Get-FileHash hash also be acquired by way of Stream 
13 is   $ hashWeb = the Get-FileHash -InputStream ( $ Stream ) - Algorithm the MD5
 14   # note off the MemoryStream 
15   $ Stream .Close ()
 16   the Write-the Output $ hashWeb 
. 17  
18 is   $ hashLocal .hash -eq  $ hashWeb .hash

2. The content line by line comparison of the two documents are the same

1 $path = "C:\local.txt"
2 $url = "XXX"
3 $contentLocal = Get-Content $path
4 $cotentWeb = Invoke-WebRequest -Uri $url | select -ExpandProperty Content
5 $diff = Compare-Object -ReferenceObject $($contentLocal) -DifferenceObject $($cotentWeb)
6 if($diff) {
7     Write-Output "The content is not the same!"
8 }

Run findings are incorrect, debugging find  Get-Content( cat) return value type System.Array , and Invoke-WebRequest the return value type is String

 1 PS C:\> $item1.GetType()
 2 
 3 IsPublic IsSerial Name                                     BaseType
 4 -------- -------- ----                                     --------
 5 True     True     Object[]                                 System.Array
 6 
 7 PS C:\> $item2.GetType()
 8 
 9 IsPublic IsSerial Name                                     BaseType
10 -------- -------- ----                                     --------
11 True     True     String                                   System.Object

 It is necessary to Invoke-WebRequest return the value of the type of conversion

path $ = " C: \ local.txt " 
$ url = " XXX " 
$ contentLocal = the Get-Content $ path 
$ cotentWeb = the Invoke-WebRequest -uri $ url | the SELECT - ExpandProperty Content
 # using regular expressions "\ r \? Effect n "newline eliminate the difference 
$ cotentWebArray = $ cotentWeb the -split '? \ R & lt \ n-'
 $ the diff = the Compare-Object -ReferenceObject $ ( $ contentLocal ) -DifferenceObject $ ( $ cotentWebArray )
 IF ( $ the diff ) { 
    the Write - the Output "The content is not the same!"
}

Guess you like

Origin www.cnblogs.com/makesense/p/11184554.html