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

时间:2014-09-03 17:29:17

标签: javascript powershell

我使用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>结果"),我的脚本没有做任何事情(&#34中列出的文件;结果"未创建):

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

感谢任何帮助,

由于

1 个答案:

答案 0 :(得分:1)

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();
相关问题