首页 文章

Powershell:运行批处理文件并将输出写入文本文件

提问于
浏览
1

我在我的Powershell脚本中有一个批处理文件,我想运行它并将其输出写入文本文件 . 我尝试了以下方法:

Start-Process c:\path\file.bat | Out-File c:\path\output.txt

我也尝试使用Tee-Object和Write-Output代替Out-File,但没有任何内容写入文本文件 .

2 回答

  • 0

    您可以使用 Start-TranscriptStop-Transcript 让PowerShell控制台将所有控制台输出写入文本文件 .

    像这样使用它:

    Start-Transcript -Path c:\temp\log.txt
    &c:\path\file.bat 
    Stop-Transcript
    

    有关详细信息,请参阅the documentation .

  • 2

    如果你想要bat文件的输出,例如构建脚本的结果,然后我发现以下效果最好:

    $Product = 'ProductName' 
    &C:\Path\$Product.bat | Out-File C:\Path\$Product-Output.txt
    

相关问题