Displaying output of compare-object in textbox

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5827

    by Davehal at 2013-04-21 06:09:12

    Hi,

    I am new to powershell and am trying to do a simple (I thought :-P) comparison between the contents of two text files and display them in an output textbox.

    I have two textboxes called Diff1 and Diff2, these contain the paths to the files i want to compare. I then click a button called compare. I want to display the result in a textbox called console:

    code[$Compare_OnClick=
    {
    #TODO: Place custom script here
    $comp1 = Get-ChildItem -Recurse -Path $Diff1.text;
    Write-Host "comp1 = " $comp1;
    $console.AppendText("comp1 = " + $comp1);
    $console.AppendText("`r`n");

    $comp2 = Get-ChildItem -Recurse -Path $Diff2.text;
    Write-Host "comp2 = " $comp2;
    $console.AppendText("comp2 = " + $comp2);
    $console.AppendText("`r`n");

    $compOutput = Compare-Object $(Get-Content $comp1) $(Get-Content $comp2)

    Write-Host $compOutput;
    $console.AppendText((Compare-Object $(Get-Content $comp1) $(Get-Content $comp2)));
    $console.AppendText("`r`n");
    }]

    The powershell console (output of Write-Host) displays everything correctly
    I can see the two paths in the textbox no problem, but the result of the comparison ($compOutput) is not shown in the textbox.

    Does anyone know what I'm doing wrong? Or have any advice? I can't figure out why I can display some things but the output of Compare-Object!!!

    Thanks in advance

    Dave

    by MasterOfTheHat at 2013-04-22 07:00:59

    I'm guessing that $console is your text box. Do you see the "comp1 = comp1" and "comp2 = comp2" lines in the text box? And is there any reason you are running the Compare-Object command twice?

    by Davehal at 2013-04-22 07:49:49

    Hi,

    Thanks for your reply,

    Yes, $console is my textbox, its just a large textbox on the qui.
    I can see the output of comp1 = comp1 and comp2 = comp2 in this textbox.
    I was editing the code when trying to track down the issue, that's why the Compare-Object command is there twice, I forgot to correct it!

    I've pasted in a correct version of my code:

    $Compare_OnClick=
    {

    $comp1 = Get-ChildItem -Recurse -Path $Diff1.text;
    Write-Host "comp1 = " $comp1;
    $console.AppendText("comp1 = " + $comp1);
    $console.AppendText("`r`n");

    $comp2 = Get-ChildItem -Recurse -Path $Diff2.text;
    Write-Host "comp2 = " $comp2;
    $console.AppendText("comp2 = " + $comp2);
    $console.AppendText("`r`n");

    $compOutput = Compare-Object $(Get-Content $comp1) $(Get-Content $comp2);

    Write-Host $compOutput;
    $console.AppendText($compOutput);
    $console.AppendText("`r`n");
    }

    Thanks!!!

    by MasterOfTheHat at 2013-04-22 08:18:47

    Np! Btw, try using the Code or powershell tags to make the posts more readable...

    And check out the help for Compare-Object, (help compare-object -full):
    OUTPUTS
    None, or the objects that are different

    When you use the PassThru parameter, Compare-Object returns the objects that differed. Otherwise, this cmdlet
    does not generate any output.

    So try adding -PassThru to the end of your Compare-Object command.

    by Davehal at 2013-04-22 08:32:44

    Thanks very much, that worked – I'll try RTFM in future 🙂

    $compOutput = Compare-Object $(Get-Content $comp1) $(Get-Content $comp2) -PassThru;
    $console.AppendText($compOutput);

You must be logged in to reply to this topic.