2016-04-15 36 views
0

我想輸出線,以我的.txt文件/日誌文件,基本上抓住的$source的路徑,然後說:「相比太」的$compare路徑。但是當我用Out-File這樣做時,它們會互相覆蓋。舉例來說,如果我只是在遞歸之前執行$source | Out-File $log,那麼下一個Out-File會寫入它。所以,當我看完我的日誌時,腳本的末尾就是我在下面使用的Compare-Object | Out-File $logPowershell的多張變量到Out-文件

#prompts user for input through cmd for path of log file 
    $log = Read-Host -Prompt 'Input path of log, if log does not, create one. Include file name and extension' 

    #prompts user for input through cmd, saves path, then recursively searches 
    $source = Read-Host -Prompt 'Input source path' 
    $source = gci $source -recurse 

    #prompts user for input through cmd, saves path, then recursively searches 
    $compare = Read-Host -Prompt 'Input the path of directory to compare' 
    $compare = gci $compare -recurse 

    #compares the $source to the $compare and outputs to log file 

    Compare-Object -ReferenceObject $source -DifferenceObject $compare -PassThru | Out-File $log -width 120 

回答

1

使用-Append參數將文本添加到文件而不覆蓋文件。

Out-File $log -width 120 -Append 
+0

謝謝,不知道我是如何忽略這一點的。 – user6124417

+0

沒問題,如果它回答了你的問題,你能接受答案嗎? – Richard

0

使用-Append參數將追加的文本作爲一個新的生產線。

如果您希望在一行中包含文本部分(x與y進行比較),您必須在輸出前組合字符串。例如:

"$source is compared to $compare :" | out-file $log -width 120 
$comparison | out-file $log -width 120 -Append