powershell - 使用管道将命令输出到文件
问题描述
我正在尝试输出到文件命令以删除文件并启动服务但没有机会。
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
}
解决方案
推荐阅读
- java - 一行Hello World java项目,非socket上的Socket操作:configureBlocking
- ios - 当 iOS 虚拟键盘出现在屏幕上时,是否有已知的解决方法可以解决在错误位置渲染的固定元素?
- html - 如何在 Microdata 中组合两种 Schema.org 类型?
- laravel - 2 模型 2 foreach 在 1 个视图中。拉拉维尔
- protocol-buffers - 资源内集合上的协议缓冲区字段掩码
- r - R: 使用 unlist() 函数来处理包含整数 (0) 元素的列表?
- c - 我如何错误地使用 void 表达式?
- bash - 如何在linux中通过比较删除两个目录中的相同文件?
- ckeditor4.x - 在 eo.editor.js > storageManager 中,“type: local”属性是什么意思?
- python - 在 python 中使用 seaborn 在分布图上显示峰度、偏度等指标