2014-09-03 140 views
0

我正在使用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>結果」),我的腳本沒有做任何事情(在 「結果」 列出的文件不被創建):

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

任何幫助表示讚賞,

由於

+0

我不會從量子物理知道java,但是如果沒有別的東西能不能在PowerShell生成它之後重命名輸出文件? – TheMadTechnician 2014-09-03 17:46:36

+0

我試圖更新您的代碼格式。我注意到,這兩個代碼片段都沒有關閉'} – Matt 2014-09-03 18:43:33

+0

「}」「。謝謝 – user3566591 2014-09-03 18:51:38

回答

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

這工作正常,謝謝 – user3566591 2014-09-04 00:57:00