首页 > 解决方案 > 使用管道将命令输出到文件

问题描述

我正在尝试输出到文件命令以删除文件并启动服务但没有机会。

get-childitem -path $cleanpath -recurse | remove-item -force -Recurse >> $log
Start-Service -Name "Bamboo" >> $log

我尝试过这样的事情,但也失败了:

get-childitem -path $cleanpath -recurse | remove-item -force -Recurse | Tee-Object -FilePath $log -Append
Start-Service -Name "Bamboo" | Tee-Object -FilePath $log -Append

我尝试使用以下方式运行 powershell 脚本 :.\clean-cache-folder.ps1 *>&1 output1.txt.\clean-cache-folder.ps1 | Out-file .\output1.txt.\clean-cache-folder.ps1 >> .\output1.txt

所有这些方法都失败了。日志文件为空。请帮忙。

另一种给出空日志文件的方法

try
{
Write-Output "===============================" >> $log
Get-Date -Format G >> $log
Remove-Item -Path $cleanpath -force -Recurse | Out-File $log -Append
#Start-Service -Name "Bamboo" >> $log
}
catch 
{
#Write-Warning $Error[0] >> $log
#Write-host ($_ | Tee-Object -FilePath $log -Append)
Write-Output ($_) >> $log
}

标签: powershellloggingoutput

解决方案


推荐阅读