Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying numerous functions in Powershell including Comparison Object, for each function but with no success.

This is what I want to do.

I have two files named as File 1 and File 2. Within File 1, I have a line like this.

In File 2, I have this.

So I want to compare the value of ContractGroups from File 1 to the value of ContractGroups in file 2. If matched, I want to add an outcome to the file.

The difficulty I am having is that I can set up a variable to get the content for matching. But when comparing, I want to only take into account of ContractGroups for a match. So as soon as file 2 is scanned, then I want to see if the value of ContractGroups matches to file 1.

I think for comparison, I just need to find a way of scanning the line that contains Name="ContractGroups" Value from file 1 and then reading its value (CC) and then scanning file 2 to first see if has a line matches Name="ContractGroups" Value and if yes, and then does the value of it is the same (CC) as file one? If yes, then just write that "Matched" to a blank file.

It would be helpful if you shed light on this. Sorry as I am new on PS.

What I have tried:

I have tried this.
$file1 = Get-Content "C:\Users\Altunokc\Desktop\Guardian\NotUsed\20200513 Environment parameters\EnvironmentParameters_ICE_EU_PROD.xml" 
$file2 = Get-Content "C:\Users\Altunokc\Desktop\inetpub\wwwroot\EU\Guardian\Website\appSettings.config"
$pattern = $file1| %{$_ -MATCH   '.Name="ContractGroups" Value'}


$result = "C:\Users\Altunokc\Desktop\Differentdd.txt"

$file1 | foreach { $pattern = $file2 -match $_

        if ( $match ) { $match | Out-File -Force $result -Append }
       
      }

and this way
if(Compare-Object -ReferenceObject $(Get-Content $file2 )   -DifferenceObject $(Get-Content $file1 ) | %{$_ -MATCH   '.Name="ContractGroups" Value'}  -SimpleMatch|
Out-File  -FilePath C:\Users\Altunokc\Desktop\Different.txt)

 {"match"}
Else  {"No "}

No luck.
Posted
Updated 21-Jun-20 22:26pm
v2
Comments
Richard MacCutchan 22-Jun-20 5:11am    
What does "No luck" mean? And what are the values of the items that you are comparing?
Cihan Altunok 22-Jun-20 5:13am    
Hi Richard, I was unable to just compare the value for ContractGroups and not the whole line of the files. The value for comparison. is "CC"
Thanks a lot
Richard MacCutchan 22-Jun-20 5:19am    
Sorry but I have no idea what that means. The issue is that you need to find the basic items from each file. So rather than trying to do it all in on single statement, break it down into stages. Find the item(s) that you are looking for in file1. If you find them then print them to the screen so that you know that part of the code is working. Do the same for file 2. You should now be able to see how to compare the two values.
Cihan Altunok 22-Jun-20 8:53am    
$reader = New-Object System.IO.StreamReader("C:\Users\Altunokc\Desktop\Guardian\NotUsed\20200513 Environment parameters\EnvironmentParameters_ICE_EU_PROD.xml")
$comparator = New-Object System.IO.StreamReader("C:\Users\Altunokc\Desktop\inetpub\wwwroot\EU\Guardian\Website\appSettings.config")

$lines = @()

if ($reader -ne $null) {
While (!$reader.EndOfStream ) {
$line = $reader.ReadLine()

if ($line.Contains('SettingId="16"') -AND $line.Contains('ContractGroups') ) {

$lines += $line

$lines | Select-Object -Last 1


}
}
}

$lines2 = @()

if ($comparator -ne $null) {
While (!$comparator.EndOfStream ) {
$line2 = $comparator.ReadLine()

if ($line2.Contains('ContractGroups"')) {

$lines2 += $line2

$lines2 | Select-Object -Last 1


}
}
}
Cihan Altunok 22-Jun-20 9:00am    
As you can see above, I can get the values like below now.
add key="ContractGroups" value="CC"/>
<setting settingid="16" name="ContractGroups" value="CC">

I suppose I just need to find a way of comparing the bit after value so the logic will be like

If value ( read value from line 1) es equal to the value from file 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900