2016-04-15 33 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