JavaScript/PowerShell:比较两个文件并将结果保存到文本文件

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

本文关键字:文件 结果 保存 文本 两个 PowerShell 比较 JavaScript      更新时间:2023-09-26

我使用JavaScript运行PowerShell命令来比较两个文件;结果保存到一个文本文件:

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();

这个代码运行得很好;但是,我必须根据一个变量动态命名Results.txt文件:

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

如果我更改PowerShell代码以包含变量("-PassThru>Results"),则我的脚本不会执行任何操作(未创建"Results"中列出的文件):

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();

感谢您的帮助,

感谢

Results需要连接到第三个代码段中的CommandPS。它应该是这样的:

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();