Compare Scripts

Hi

I have following 2 arrays that I need to compare using compare-object:

$Version1 = ‘1.12.48.00’

$Version2 = ‘1.12.47.02’

$array = @(’\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm’,

        '\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')

$array2 = @(’\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm’,

        '\\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')

$array_version1 = $array -replace ‘Version1’ , $Version1

$array_version2 = $array2 -replace ‘Version2’ , $Version2

foreach ($i in $array_version1){

    foreach ($ii in $array_version2){
     write-output ("array_version1" + $i)
     Write-output ("array_version2" + $ii)

– Compare-Object -ReferenceObject (Get-Content -Path $array_version1) -DifferenceObject (Get-Content -Path $array_version2)

     }

I’m having ISSUE WITH SETTING UP CORRECT LOOP WITHIN LOOP TO GET DESIRED OUTPUT. THE OUTPUT IS FOLLOWING:

array_version1\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm

array_version2\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm

array_version1\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm

array_version2\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm

array_version1\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm

array_version2\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm

array_version1\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm

array_version2\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm

Could you please go back and correct the formatting of the code in you post? Simply click on the icon for preformatted text ( </> ) in the post editor and paste your code where it’s telling you to do.
Please edit your original post - do not create a new one.
Thanks in advance.

$Version1 = '1.12.48.00'

$Version2 = '1.12.47.02'

 

$array  = @('\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm',

            '\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')

 

$array2 = @('\\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm',

            '\\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')

 

$array_version1 = $array -replace 'Version1' , $Version1

 

$array_version2 = $array2 -replace 'Version2' , $Version2

 

 

foreach ($i in $array_version1){

       foreach ($ii in $array_version2){



       write-output ("array_version1" + $i)

      Write-output ("array_version2" + $ii)

-- -          Compare-Object -ReferenceObject (Get-Content -Path $array_version1) -DifferenceObject (Get-Content -Path  $array_version2)

        }


 }

hmmm … not quite what I asked for but anyway …

I’n unsure what you’re actually trying to do. :thinking: And I don’t know why you’re screaming. :unamused:

If you want to compare two objects with Compare-Object you don’t need a loop.

$Version1 = '1.12.48.00'
$Version2 = '1.12.47.02'
$array  = @('\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm',
            '\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')
$array2 = @('\\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm',
            '\\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm')

$array_version1 = $array -replace 'Version1' , $Version1
$array_version2 = $array2 -replace 'Version2' , $Version2

Compare-Object -ReferenceObject $array_version1 -DifferenceObject $array_version2

What’s producing following output:

InputObject                                                                           SideIndicator
-----------                                                                           -------------
\\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm      =>
\\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm    =>
\\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm <=
\\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm    <=

Regardless of that - Get-Content is used to read the content of a given file. If you want to access the content of a variable you simply refence it with the dollar sign. Just like you already did with $Version1, $array, $array_version2 and so on … :wink:

Hi Olaf

That is exactly i want. I want to compare the files and it’s content off array_vession1 to array_vession2.

If the file in array_version1 exists in array_version2 then compare the content of the files and output the differences only.

The files are in different version i.e
$Version1 = ‘1.12.48.00’
$Version2 = ‘1.12.47.02’

the files in $Version1 i.e

‘\hqlosroot\d$\Updates*Version1*\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm’

will be matched with

$Version2 = ‘1.12.47.02’ i.e

‘\hqlosroot\d$\Updates*Version2*\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm’

Also

and also for

$Version1 = ‘1.12.48.00’

\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm

compare with

$Version2 = ‘1.12.47.02’ i.e

‘\hqlosroot\d$\Updates\Version2\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm’.

The DeliverMornet-Copy.scm in $Version1 is copy off whats was already in

$Version1 and needs to be matched with

DocutechRequest.scm in

$Version2.

I think I got it. Your variable names are a little bit missleading.

If it’s really just these two comparisons it’s actually easier to understand if you write your code quite explicit like this:

$File1Version1 = Get-Content -Path '\\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DeliverMornet.scm'
$File2Version1 = Get-Content -Path '\\hqlosroot\d$\Updates\1.12.47.02\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm'
$File1Version2 = Get-Content -Path '\\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm'
$File2Version2 = Get-Content -Path '\\hqlosroot\d$\Updates\1.12.48.00\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm'

Compare-Object -ReferenceObject $File1Version1 -DifferenceObject $File1Version2
Compare-Object -ReferenceObject $File2Version1 -DifferenceObject $File2Version2

If it’s actually more dynamic or if there actually more files to compare you could use a kind of loop like this:

$VersionList = '1.12.48.00', '1.12.47.02'
$FileList = '\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm',
            '\\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm'
foreach ($File in $FileList) {
    $FilePath1 = $File -replace 'Version1', $VersionList[0]
    $FilePath2 = $File -replace 'Version1', $VersionList[1]
    $Content1 = Get-Content -Path $FilePath1
    $Content2 = Get-Content -Path $FilePath2
    Compare-Object -ReferenceObject $Content1 -DifferenceObject $Content2
}

In my opinion it just does not make sense to use a nested loop because you would end up with combination you actually don’t need.

Hi Olaf

I modified the script below to what i wanted. You put me in right direction thanks so much.

$VersionList = ‘1.12.48.00’, ‘1.12.47.02’
$FileList = ‘\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DeliverMornet-Copy.scm’,
‘\hqlosroot\d$\Updates\Version1\QA\exe\DestinyLOS\Scheme\App\DocutechRequest.scm’
foreach ($File in $FileList) {
$FilePath1 = $File -replace ‘Version1’, $VersionList[0]
$FilePath2 = $File -replace ‘Version1’, $VersionList[1]
$Content1 = Get-Content -Path $FilePath1
$Content2 = Get-Content -Path $FilePath2
Compare-Object -ReferenceObject $Content1 -DifferenceObject $Content2
}

Great.

But what’s so hard on formatting the code as code here in the forum? :thinking: It’s really just on click more.

BTW: What’s the difference between my code suggestion and the code you posted last? … except for typographic quote charachters you’re using it’s the same!?! :face_with_raised_eyebrow:


    $FilePath1 = $File -replace 'Version1', $VersionList[0]

    $FilePath2 = $File -replace 'Version1', $VersionList[1]

    $FilePath3 = Get-ChildItem -Path $FilePath2

    $Content1 = Get-Content -Path $FilePath1

    $Content2 = Get-Content -Path $FilePath3

    Compare-Object -ReferenceObject $Content1 -DifferenceObject $Content2 |Out-File -FilePath .\Process2.txt -Append

}

Olaf I added get-childitem in the above script.

I have another question. I’m getting this in my Process2.txt:

InputObject SideIndicator


            =>

            =>

“This is test” <=

“This is test2” <=

InputObject SideIndicator


(define postUrl-test EngineWS Web Service) =>

(define postUrl-test EngineWS Web Service) <=

I want to format the output to be more meaninful. That is it contains the file name associated with each output that are different. How do i do that?

Again thanks olaf!