user3566591
user3566591

Reputation: 165

JavaScript/PowerShell: Compare Two files and Save Results to a Text File

I am using JavaScript to run a PowerShell command in order to compare two files; the results are saved to a text file:

function RunPowerShell() { var CommandPS= ' $File1 = Get-Content
"C:\\Test\\test1.txt"; $File2= Get-Content 
"C:\\Test\\test2.txt";Compare-Object $File2 $File1 -PassThru
"C:\\Test\\Results.txt"';

var CmdCommand= 'cmd /c PowerShell '+ CommandPS;
}

RunPowerShell();

This code run just fine; however, I have to name my Results.txt file dynamically, based on a variable:

var AssignedNumber=1 var
Results='C:\\Test\\Results'+'_'+AssignedNumber+'.txt';

If i change the PowerShell code to include the variable ( "-PassThru > Results"), my script is not doing anything (the file listed in "Results" is not created):

function RunPowerShell() { var CommandPS= ' $File1 = Get-Content
"C:\\Test\\test1.txt"; $File2= Get-Content 
"C:\\Test\\test2.txt";Compare-Object $File2 $File1 -PassThru >
Results;

var CmdCommand= 'cmd /c PowerShell '+ CommandPS;
   }  
RunPowerShell();

Any help is appreciated,

Thanks

Upvotes: 0

Views: 195

Answers (1)

Eric Eskildsen
Eric Eskildsen

Reputation: 4769

Results needs to be concatenated to CommandPS in the third snippet. It should look like this:

function RunPowerShell() {
    var CommandPS= ' $File1 = Get-Content 
    "C:\\Test\\test1.txt"; $File2= Get-Content 
    "C:\\Test\\test2.txt";Compare-Object $File2 $File1 -PassThru > "' + Results + '";'

    var CmdCommand= 'cmd /c PowerShell '+ CommandPS;
}  
RunPowerShell();

Upvotes: 1

Related Questions